0

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

demo
  • 5,676
  • 16
  • 65
  • 141

2 Answers2

2

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

Alexandre Elshobokshy
  • 10,357
  • 6
  • 25
  • 53
mariallery
  • 21
  • 1
  • 4
0

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
              });
Vipul
  • 1,783
  • 1
  • 14
  • 12