角指令,属性更新时,链接不叫 [英] Angular directive, link not called when attribute updates
问题描述
在下面的例子:<一href=\"http://plnkr.co/edit/OZjg6sUgl35GIriaabQg?p=$p$pview\">http://plnkr.co/edit/OZjg6sUgl35GIriaabQg?p=$p$pview
我有2指示, showCard
之一的NG重复这链接
函数被调用的内部时间属性被更新。 (见控制台)
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)
另一种 showCards
工作正常,但链接
当属性被更新函数不叫,但只有一次在开始
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.
我想了解这些2种之间的差异。
I'd like to understand the difference between those 2 kind.
推荐答案
链接功能仅每个元素调用一次,所以每当你添加新卡ngRepeat,指令将添加一个新的&LT;显示卡...&GT;
将调用链接功能。
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.
如果您想进行一些功能触发每次卡
更改您可以在showCards链接功能范围增加$手表的功能,如:
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);
这篇关于角指令,属性更新时,链接不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!