Tuesday 15 April 2014

javascript - Angular Directives - Knowing when the object bound to an attribute is changed -



javascript - Angular Directives - Knowing when the object bound to an attribute is changed -

assume have scope displays list of users on left , detailed info selected user on right.

i create user details element directive 2 way binding , end directive object looks this:

{ restrict: 'e', scope: { user: "=" }, templateurl: '...', controller: function() { ...elided...} }

in parent html, utilize directive as:

<user-details user="currentuser"></user-details>

as user selection changes on left, currentuser property updated, causes user property in directive point different object. angular handles re-rendering html in user-details directive well,

(here's question)

supposing have little bit of work need when displayed user changed, perhaps json string should parsed object.

how know bound object has changed (and need re-parse)?

i have tried:

watching $scope.user observing $attr, 'user'

the controller , link functions appear called once.

somewhere must missing something. doesn't seem should far off of beaten path.

you should utilize link , watch user this:

{ restrict: 'e', scope: { user: "=" }, templateurl: '...', link: function(scope) { scope.$watch('user',function(value){ // stuff here }); } }

you watch user function well:

scope.$watch(function(){ retrun scope.user; }, function(value){ // stuff here });

but updated on digests. can check documentation or search angularjs digest cycle.

edit:

i forgot scope argument since re-create pasted.

javascript angularjs

No comments:

Post a Comment