Bindonce with ng-repeat

213 Views Asked by At

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

2

There are 2 best solutions below

0
On BEST ANSWER

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
              });
0
On

Most probable cause: your function getRequests in CurrectUserData doesn't return a promise, it should be return $http.get('/the/url/etc')