Why is a function in ng-repeat called several times?

后端 未结 2 1708
梦谈多话
梦谈多话 2021-01-21 06:47

I want to supply a ng-repeat element by a controller function as follows:

2条回答
  •  迷失自我
    2021-01-21 07:26

    I would really avoid calling a function insides a ngRepeat attribute, since it will give errors and unexpected behaviour.

    But to be honest I dont think that you would need to call a function inside a ngRepeat. I would suggest to do the following:

    $scope.getPictures = function(pictures) {
            alert("function called");
            //return... extract all pictures and return as array
    };
    
    $scope.allPictures = $scope.getPictures();
    

    This way the $scope.getPictures function will get called and the $scope.allPictures will be created. ngRepeat can call that collection instead of a function.

    See also my Fiddle: https://jsfiddle.net/ABr/w6kc8qyh/1/

提交回复
热议问题