Right, I see what you mean.  I definitely thought that type of assignment was 
by reference.

Can you tell me then why when I push or pop a new element to the services, my 
controller reference is updated accordingly?  Are arrays passed by reference 
and objects not?

As I typed that I realized I could easily search and find out myself…

Thanks for the help!



> On Dec 1, 2014, at 8:19 PM, Tony pee <[email protected]> wrote:
> 
> The problem is with your understanding of how references work in javascript i 
> feel. In a small example:
> 
> var obj = { prop: 123 }; an object with a property
> var ref = obj.prop;
> 
> ref = 456; // this will NOT mean that  obj.prop == 456, it means that ref is 
> equal to a primitive value of 456. obj.prop is untouched. 
> 
> In your example, when you do lines like this: 
> 
> $scope.model.allModel = MainSvc.getModel(); 
> 
> you set on $scope.model (a different object) a reference to the object which 
> is ALSO stored in your model class. when you change the value of $scope.model 
> you just change what that reference is pointing to. The object referenced in 
> your service is untouched. You should try directly referencing the value 
> stored on the model. And you dont really want to set it on the controller, 
> otherwise you'd need to also update it on the service to make it share 
> between controllers (original purpose). So its better to prove that it can be 
> shared:
> 
> http://plnkr.co/edit/Q054Lw0Mi80LRnCBKlg6?p=preview 
> <http://plnkr.co/edit/Q054Lw0Mi80LRnCBKlg6?p=preview>
> 
> I changed the style to a more class like style. And kept your getters and 
> setters, altho they arent really needed (see the direct method works too)
> 
>  
> 
> On 1 December 2014 at 18:05, Jonathan Price <[email protected] 
> <mailto:[email protected]>> wrote:
> I've got a service that has a collection of things.  It also has an 
> individual thing of the same type.  I'll use a service like this to pass 
> around between controllers when I have a collection of things that I'll 
> individually want to update.    Imaging a collection of say books.  I can 
> display the collection through a table, click on one to edit, update it and 
> reload the entire collection..
> 
> I've got a plunker here that shows what I'm confused by:
> 
> http://plnkr.co/edit/P2smRGGAjEIHbZyAAKKR?p=preview 
> <http://plnkr.co/edit/P2smRGGAjEIHbZyAAKKR?p=preview>
> 
> I don't understand why my controller's scope isn't staying directly tied to 
> the service values?  I thought they became associated by reference when I set 
> them equal to one another in my controller?  
> 
> Thanks!
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] 
> <mailto:[email protected]>.
> To post to this group, send email to [email protected] 
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/angular 
> <http://groups.google.com/group/angular>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.
> 
> 
> 
> -- 
> Tony Polinelli
> 
> 
> -- 
> You received this message because you are subscribed to a topic in the Google 
> Groups "AngularJS" group.
> To unsubscribe from this topic, visit 
> https://groups.google.com/d/topic/angular/DXIj_XsrqDM/unsubscribe 
> <https://groups.google.com/d/topic/angular/DXIj_XsrqDM/unsubscribe>.
> To unsubscribe from this group and all its topics, send an email to 
> [email protected] 
> <mailto:[email protected]>.
> To post to this group, send email to [email protected] 
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/angular 
> <http://groups.google.com/group/angular>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to