Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ng-repeat complete in angular directive

In the code below, how does the angular directive 'myscroll' know what ng-repeat elements are being created?

<myscroll>
     <div ng-repeat="a in arr">{{a}}</div>
</myscroll>

I know that the $last event is not fired to the parent directive, how can I solve this?

myapp.directive("myscroll", function () {
    return{
        restrict: "E",
        transclude: true,
        template: "<span class='left'></span><div class='mask' ng-transclude></div><span class='right'></span>",
        link: function (scope, element, attr) {

            scope.$watch("$last",function(){ console.log("ng-repeat rendered") })

        }
    }
})
like image 988
Dinesh Avatar asked Jun 19 '26 19:06

Dinesh


1 Answers

A simple way to do it is to create a new directive called 'repeat-done' and use it where the 'ng-repeat' is. Then you can notify the 'myscroll' directive (parent scope) whatever you need.

<myscroll>
     <div ng-repeat="a in arr" repeat-done>{{a}}</div>
</myscroll>

myapp.directive('repeatDone', [function () {
  return {
    restrict: 'A',
     link: function (scope, element, iAttrs) {

          var parentScope = element.parent().scope();

          if (scope.$last){
               // ng-repeat is completed and you now have access to 'myscroll' scope
               console.log(parentScope);   
          }
        }
      };
    }])
like image 153
Denison Luz Avatar answered Jun 22 '26 08:06

Denison Luz