Demo plunker
Implement an isolated scope directive to encourage modularity and reuse:
app.directive('myMenuList', function($timeout) { return { restrict: 'A', scope: { myMenuList: '=' }, link:function($scope, $element, $attr) { $timeout(function(){ $scope.myMenuList.numerofli= $element.find('li').length ; $scope.myMenuList.ulwidth= $element[0].clientWidth; }, 1000); } } });
To use it, initialize the output model from inside the parent controller:
app.controller('scrollController', function($scope, $timeout) { $scope.output = {}; ... });
Put the my-menu-list attribute on the ul element and pass it the previously defined model:
<ul my-menu-list="output" ng-style="myStyle"> <li ng-repeat="item in items"> <a href="#">{{item.name}}</a> </li> </ul>
When executing the directive, it will populate the model with two properties that you can reference in your HTML:
<p><b>Numer of 'li': {{output.numerofli}}</b></p> <p><b>Width: {{output.ulwidth}}</b></p>
source share