1

In a directive I need to have the width of an element, however this element is created in a ng-repeat. So when the code inside my link function runs, ngRepeat is not yet executed. I tried to delay things using $timeout but that didn't help

JS:

link: function (scope, element) {
    $timeout(function () {
        var width = $(element.find('li')).width(); // the li elements do not exist
    });
}

HTML:

<ol>
    <li ng-repeat="item in items">
        <my-item>.....</my-item>
    </li>
</ol>

I cannot use the <ol> element, because the <my-item> has styles like margins/paddings.

Any suggestions ?

Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333
  • check this out, [link1](http://stackoverflow.com/questions/13471129/angularjs-ng-repeat-finish-event) and [link2](http://stackoverflow.com/questions/15207788/calling-a-function-when-ng-repeat-has-finished) – Yuliam Chandra Jul 10 '14 at 10:00

2 Answers2

3

From ng-repeat finish event

<ol>
    <li ng-repeat="item in items" on-complete>
        <my-item>.....</my-item>
    </li>
</ol>

-

.directive('onComplete', function() {
  return function(scope, element, attrs) {
    angular.element(element).css('color','blue');
    if (scope.$last){
      window.alert("im the last!");
    }
  };
})

http://plnkr.co/edit/or5mys?p=preview

Community
  • 1
  • 1
Bolza
  • 1,904
  • 2
  • 17
  • 42
1

You could use the scope.$last to generate an event when the last item is done or just run the code you need in there.

You might also want to consider your design. Could there be any CSS solution for your problem? If not go ahead.

Community
  • 1
  • 1
Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96