or if you want to wrap the service in methods at the controller level
(often a good idea)

http://plnkr.co/edit/2OxsdgZYozIIutVGEqYf?p=preview

On 1 December 2014 at 20:24, Tony pee <[email protected]> wrote:

> This is a more minimal example, how id name things (no pun):
>
> http://plnkr.co/edit/qpWKOh4zSMtTGgjmWYV0?p=preview
>
> On 1 December 2014 at 20:19, 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
>>
>> 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]>
>> 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
>>>
>>> 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].
>>> 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.
>>>
>>
>>
>>
>> --
>> Tony Polinelli
>>
>>
>
>
> --
> Tony Polinelli
>
>


-- 
Tony Polinelli

-- 
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