Javascript AngularJs在视图渲染后执行函数
我是新来的Angularjs, 我试图写一个函数,可以自动运行后ng重复渲染完成。 这是我在网上找到的一个函数Javascript AngularJs在视图渲染后执行函数,javascript,angularjs,Javascript,Angularjs,我是新来的Angularjs, 我试图写一个函数,可以自动运行后ng重复渲染完成。 这是我在网上找到的一个函数 angular('myController',['myDirective']).controller([ '$scope', 'myService', function($scope,myService){ $scope.List = {}; var list = myService.get({}, function Success(){
angular('myController',['myDirective']).controller([
'$scope',
'myService',
function($scope,myService){
$scope.List = {};
var list = myService.get({},
function Success(){
$scope.List = list;
}
);
]};
angular('myDirective',[]).directive('myList',
['$timeout',
function ($timeout) {
return {
restrict: 'A',
link: function (scope, elem, attrs, ctrl) {
if (scope.$last === true) {//make sure the view is rendered
$timeout(fn.ListIsReady, 0);
}
}
}
}]);
这是确保fn.ListIsReady在视图渲染后执行的唯一方法吗?
我会问这个问题,因为每次渲染项目时都会执行函数“mydirective.link”。我认为这不明智。您可以设置一个
$watchCollection
来监视列表何时更改,然后在渲染阶段后使用$timeout
执行函数:
angular('myController',['myDirective']).controller(['$scope', '$timeout','myService',
function($scope,$timeout, myService){
$scope.List = {};
var list = myService.get({},
function Success(){
$scope.List = list;
}
);
$scope.$watchCollection('List', function(newList) {
if (newList)
$timeout(fn.ListIsReady);
});
]};
真正地但是我试图跟踪执行时间,它跟踪与列表大小相同的数字。