Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular directive, link not called when attribute updates

Tags:

angularjs

In the following example: http://plnkr.co/edit/OZjg6sUgl35GIriaabQg?p=preview

I've got 2 directives, the showCard one inside an ng-repeat which link function get called any time the attribute is updated. (see the console)

The other one showCards is working properly but the link function is not called when the attribute is updated, but only once at the beginning.

I'd like to understand the difference between those 2 kind.

like image 981
plus- Avatar asked Apr 16 '13 13:04

plus-


1 Answers

The linking function is only invoked once per element, so whenever you add a new card the ngRepeat-directive will add a new <show-card ...> which will invoke the link function.

If you want some function to trigger every time cards is changed you can add a $watch function on the scope in the showCards link function, like this:

   $scope.$watch('cards',function(){
     console.log('multi',$scope.cards);
   },true);
like image 73
joakimbl Avatar answered Sep 20 '22 01:09

joakimbl