最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - {{$index}} of ng-repeat computed after linker function of angular directive. $compile it? - Stack Overflow

programmeradmin1浏览0评论

html

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

angularjs directive:-

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

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

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

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

$pile(element)(scope) 

is the syntax. But obviously the wrong order.

html

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

angularjs directive:-

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

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

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

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

$pile(element)(scope) 

is the syntax. But obviously the wrong order.

Share edited Oct 8, 2013 at 17:55 Sangram Singh asked Oct 8, 2013 at 17:47 Sangram SinghSangram Singh 7,19115 gold badges51 silver badges79 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

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.

Your directive is already in the process of being piled when and linked when your link function is reached so I think repiling 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.

Example here: http://plnkr.co/edit/n77x4C?p=preview

I hope this will help you

  1. directive

     angular.module('time', []).directive('repeater', function() {
       return{
              restrict:"E",
              scope:{
                    itemarray: "=itemarray"
                 }
              template:"<div ng-repeat='item in itemarray' class='first' id = '{{$index}}' > {{item}} </div>",
               link:linkFn
            }
              var linkFn = function(scope, element, attrs){
             var id = $(element).attr('id');
             alert(id); // {{$index}}
    

    } ...

  2. html

           <repeater itemarray='itemarray"></repeater>
    
发布评论

评论列表(0)

  1. 暂无评论