Search code examples
javascriptangularjsrestangular

ng-repeat does updated after restangular post request


I am using angularjs 1.5.8 my controller code is as follows

angular.module('testmodule')
 .controller('prd',function($scope,Restangular){
    this.items = [];

this.postbody = { "Filter": 700 } ;

Restangular.all('allPrd').post(this.postbody).then(function(data){
   this.items = Restangular.copy(data.tiles);
   console.log(this.items);
})

});

And my template is as follows

<div ng-controller="prd as prd2">
   <div ng-repeat='item in prd2.items>
     {{prd2.items}}  
</div>
</div>

the ng-repeat does not get updated after post request but the console log of this.items is the post request value. How do I proceed thanks in advance.

Note: I tried the answer here but it does not still work


Solution

  • Inside of then callback this is nothing but global object.

    So,this has no items.

    Instead it assign items to the global object and it is not reflected in your controller

    use var self = this

    .controller('prd',function($scope,Restangular){
    var self = this;    
    self.items = [];
    
    this.postbody = { "Filter": 700 } ;
    
    Restangular.all('allPrd').post(this.postbody).then(function(data){
       self.items = Restangular.copy(data.tiles);
       console.log(this.items);
    })
    
    });