NG-重复角指令连接功能后计算的{{$指数}}。 $编译它? [英] {{$index}} of ng-repeat computed after linker function of angular directive. $compile it?

查看:140
本文介绍了NG-重复角指令连接功能后计算的{{$指数}}。 $编译它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML

<div repeater ng-repeat='item in items' class='first' id = '{{$index}}' > {{item}} </div>

angularjs指令: -

angularjs directive:-

angular.module('time', [])
  .directive('repeater', function() {

    var linkFn = function(scope, element, attrs){
      var id = $(element).attr('id');
      alert(id); // {{$index}}
    } ...

NG-重复内创建的动态ID,指令显示诱发里面当{{$指数}},而不是值= 0,1,2 ...

dynamic id created inside ng-repeat, when evoked inside directive displays as {{$index}} instead of value = 0, 1, 2 ...

您如何确保链接器时,在指令执行的功能是用来动态IDS?我认为它可以使用 $编译指令内进行。但我不能完全弄清楚怎么样?

How do you ensure when Linker function in directive executes the dynamic ids are used? I think it can be done using $compile inside the directive. But i can't quite figure how?

$compile(element)(scope) 

是语法。但显然错误的顺序。

is the syntax. But obviously the wrong order.

推荐答案

如果你真的需要ID来已填充您可以运行在范围相关的code,$ evalAsync $超时,以确保绑定已首先更新。我建议努力,以避免需要检查DOM和而不是依赖于你的模型任何信息,使用范围。$指数

If you really need the IDs to be already populated you can run the relevant code inside scope.$evalAsync or $timeout to make sure the binding has been updated first. I would suggest trying hard to avoid needing to inspect the DOM and relying instead on your model for any of this information, using scope.$index.

您的指令已经在联系的时候,并正在编制过程中,当到达您的链接功能,所以我想重新编译不会帮助。这只是等待足够长的时间来完成连接并开始 $摘要其中的属性实际上将被更新的情况。这正是 $ evalAsync 会为你做。

Your directive is already in the process of being compiled when and linked when your link function is reached so I think recompiling wouldn't help. It is just a case of waiting long enough to finish the linking and start a $digest where the attributes will actually be updated. This is exactly what $evalAsync will do for you.

例如,在这里: http://plnkr.co/edit/n77x4C?p= preVIEW

这篇关于NG-重复角指令连接功能后计算的{{$指数}}。 $编译它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆