Angularjs: ngRepeat and directive

China☆狼群 提交于 2019-12-10 14:53:02

问题


I am trying to make some reusable countdown widgets. Works well with a static content, but when i'm trying to add them on the fly, my directive doesn't understand the variables inside the ngRepeat.

Markups:

<div ng-repeat="cdn in countdowns" class="countdown" countdown-end="{{cdn}}">
  <p ng-hide="over">{{days}} jours {{hours}} heures {{minutes}} min {{seconds}} sec</p>
  <p ng-show="over">Done</p>
</div>

Directive:

...
link: function(scope, elm, attrs) {
  scope.days = '1';
  ...
}
...

http://jsfiddle.net/hFGb7/14/

Thanks for the replies.


回答1:


The problem is that the interpolation is not run by the time link function is called. So the value of {{cdn}} is not available. There are couple of ways of handling this:

  1. You can use cdn directly in the link function since it is available on the scope. But this will make the directive dependent on the presence of cdn in the scope.
  2. The recommended way to get the value of attributes that use interpolation is to use $observe. Check this: http://jsfiddle.net/hFGb7/28/


来源:https://stackoverflow.com/questions/13526584/angularjs-ngrepeat-and-directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!