San*_*ngh 5 javascript angularjs angularjs-directive ng-repeat angularjs-ng-repeat
HTML
<div repeater ng-repeat='item in items' class='first' id = '{{$index}}' > {{item}} </div>
Run Code Online (Sandbox Code Playgroud)
angularjs指令: -
angular.module('time', [])
.directive('repeater', function() {
var linkFn = function(scope, element, attrs){
var id = $(element).attr('id');
alert(id); // {{$index}}
} ...
Run Code Online (Sandbox Code Playgroud)
在ng-repeat中创建的动态id,当在指令内部显示为{{$ index}}时,而不是value = 0,1,2 ......
如何确保指令中的链接器函数执行时使用动态ID?我认为可以在指令中使用$ compile来完成.但我不知道怎么样?
$compile(element)(scope)
Run Code Online (Sandbox Code Playgroud)
是语法.但显然错误的顺序.
如果您确实需要已填充 ID,则可以在内部运行相关代码scope.$evalAsync或$timeout确保首先更新绑定。我建议努力避免需要检查 DOM 并依赖您的模型来获取任何此类信息,使用scope.$index.
当达到链接函数时,您的指令已经在编译和链接的过程中,所以我认为重新编译没有帮助。这只是等待足够长的时间来完成链接并启动$digest属性实际更新的情况。这正是$evalAsync为您做的。
示例如下: http: //plnkr.co/edit/n77x4C ?p=preview
| 归档时间: |
|
| 查看次数: |
6538 次 |
| 最近记录: |