0

I want to call a function in ng-checked inside a ng-repeat loop, so I type ng-checked="isActive('{{item.id}}')". What I expect is if ng-checked returns true, the checkbox is checked. But somehow I get this error with unlimited loops.

[$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []

This is the controller.

$http.get('users.php').then(function(response){
  $scope.data = response.data;
});
$scope.isActive = function(id) {
     //Check if user active;
     $http.get('active_users.php?id=' + id).then(function(response){
        if (response.data == true){
           return 'true';
        }
     });

};

And in view

<div ng-repeat="item in data">
   <input type="checkbox" ng-checked="isActive('{{item.id}}')" value="{{item.id}}" /> Active
</div>          
Abaij
  • 853
  • 2
  • 18
  • 34
  • Never make ajax requests directly from view when it is not event driven. First of all they are asynchronous...not to mention your function has no return. A `return` in a callback doesn't return to the outer function. Also you don't use interpolation braces `{{}}` in view functions – charlietfl Sep 23 '17 at 22:41
  • I'm new to angular so I don't think about it. So the best practice for my case is put the ajax request in factory and call it in controller, and then in view call the function in controller? – Abaij Sep 23 '17 at 23:21
  • Best is to pass results of the requests to an object as part of scope and then reference that object in the view. Also look at using `ng-model` for what you need – charlietfl Sep 23 '17 at 23:22

0 Answers0