Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJs在视图渲染后执行函数_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJs在视图渲染后执行函数

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(){

我是新来的Angularjs, 我试图写一个函数,可以自动运行后ng重复渲染完成。 这是我在网上找到的一个函数

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);
    });
]};

真正地但是我试图跟踪执行时间,它跟踪与列表大小相同的数字。