Search code examples
angularjsangularjs-directiveangularjs-ng-repeatbindonce

Bindonce with ng-repeat


I've used Bindonce to improve performance of ng-repeat.

But I have one problem : Collection that is used from ng-repeat is filled with data a bit later (request data from API takes some time), so it's empty, cause updating is prevented by Bindonce.

How can I specify to wait response from server and then make binding?


Code example :

In controller I have array $scope.requests = [];

It is initialized with factory

$scope.requests = CurrentUserData.getRequests();

I've red about promises and thought that this code can help :

CurrentUserData.getRequests()
            .then(function(response) {
                $scope.requests = response;
            });

But I receive an error

angular.js:11655 TypeError: CurrentUserData.getRequests(...).then is not a function


Solution

  • CurrentUserData.getRequests(...) is not returning a promise.

    If you will be using the code

    CurrentUserData.getRequests()
                .then(function(response) {
                    $scope.requests = response;
                });
    

    then the line $scope.requests = response should be changed to $scope.requests = response.data

    Having said that you can make use of original code

    $scope.myData = CurrentUserData.getRequests();
    $scope.$watch(myData,
                  function(newVal, oldVal){
                         $scope.requests = newVal
                  });