hi Mauro, looking at you example if you use tab and arrow down, it doen't work. any ideas why? ex. if you tab and using the down arrow you select a, the object update to a, then select b the object does not update, then select c and the object updated to a,c
any ideas why this doesn't work? I have the same issue in my applications On Tuesday, April 30, 2013 11:03:54 AM UTC-7, Amir H. Hajizamani wrote: > > Hi Mauro, > > You've been caught out by Angular's scope inheritance hierarchy: > http://docs.angularjs.org/guide/scope#scopehierarchies > > Within each iteration of the ng-repeat a *new* scope is created which > inherits from its parent, in this case MainCtrl. These new scopes inherit > the properties and methods of your MainCtrl which is why you can call > 'addSelected' in your event handler. However, when you refer to 'selected' > in your ng-model, when the value changes it is assigned to a property > directly on these child scopes, not on MainCtrl. So in your plunker > 'addSelected(selected)' works because the child scope knows about the > method from its parent scope, and can pass in the 'selected' property > directly from itself. > > When you put your $watch code on MainCtrl's scope, it is looking for a > 'selected' model on MainCtrl which doesn't exist, and it knows nothing > about those child scopes that ng-repeat created. You need to make sure that > those child scopes refer to exactly one model that you can access on > MainCtrl, and you do this by referring to something inside an Object. So > finally (!), here is your code modified to use $watch: > http://plnkr.co/edit/TfVU4CYjzfWN2UQZmwld > > *The rule of thumb to avoid this problem is 'if you use ng-model there has > to be a dot somewhere'.* > > Watching this short video (and the two before it in the series) might help > clarify all this: The Dot http://www.egghead.io/video/DTx23w4z6Kc > For more depth, watch this: AngularJS Best Practices > http://www.youtube.com/watch?v=ZhfUv0spHCY > > Hope that helps explain things. > > Amir > > On Tuesday, 30 April 2013 17:50:37 UTC+1, Mauro Sanna wrote: >> >> I'm using ng-change on a select: >> >> http://plnkr.co/edit/dnySXwv7EX3PdHtJXQKa?p=preview >> >> I want try $watch but the code does not work. >> >> $scope.$watch('selected', function (selected) { >> $scope.selectedList.push(selected); >> }); >> > -- 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.
