0

$scope.$apply() is mostly used when $scope is defined. But this kind of definition does not seem to refresh the list.

HTML :

<div ng-repeat="feed in feedCtrl.feeds"></div>

Controller :

this.feeds = response.data;
Edison D'souza
  • 4,551
  • 5
  • 28
  • 39
  • 1
    just reassign the `this.feeds` with new data and it will be refreshed. Also you can call `$scope.$apply()` by injecting scope into controller, so that the whole digest cycle will be ran again and update the variables – Sravan Mar 29 '17 at 10:38
  • 1
    do you have some event which update the `feeds` ? – anoop Mar 29 '17 at 10:58

3 Answers3

2

Just reassign the this.feeds with new data and it will be refreshed.

Also you can call $scope.$apply() by injecting scope into controller, so that the whole digest cycle will be ran again and update the variables

In my opinion, the issue you are facing is using this.feeds inside a function which will not work, because this refers to the function but not original value.

So, assign this to a variable vm

var vm = this; and use vm.feeds = response.data

<!DOCTYPE html>
<html ng-app="myApp">

<head>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>

 <script type="text/javascript">
  var myApp = angular.module('myApp',[])
  .controller('MyCtrl', function MyCtrl($timeout,$scope) {

   var vm = this;
   vm.feeds = ["Feed1","Feed2","Feed3","Feed4"];
            
            $timeout(function () {
          vm.feeds = ["Changed Feed1","Changed Feed2","Changed Feed3","Changed Feed4"];
      }, 2000);
   
  })
 </script>
</head>
<body ng-controller="MyCtrl as feedCtrl">
<p ng-repeat="data in feedCtrl.feeds">
 {{data}}
</p>

</body>

</html>

The Code above changes the Feed after 2 seconds. Please run the above code

Here is a working DEMO

Sravan
  • 18,467
  • 3
  • 30
  • 54
0

This may help you

<body ng-app="myApp" ng-controller="appCtrl as ctrl">
<h1 ng-repeat="x in ctrl.records">{{x}}</h1>
<button ng-click="ctrl.records=['Bananna','Grapes'];">update records</button>

in controller

var app = angular.module("myApp", []);
app.controller("appCtrl", function() {  this.records = ["Mango","Apple","Orange"];    });
Sam
  • 2,275
  • 9
  • 30
  • 53
0

inject $scope in controller and use $scope.$apply() after retrieving data. No problem if you are using "this". ie

this.feeds = response.data;
$scope.$apply();

However don't forget to inject $scope in controller

Newaz Sharif
  • 424
  • 4
  • 12
  • 1
    It's bad practice to force angular for manual digest cycle. and if you need at utmost then apply safe check. `if(!$scope.$$phase) { //$digest or $apply }`. [Read more](http://stackoverflow.com/questions/12729122/angularjs-prevent-error-digest-already-in-progress-when-calling-scope-apply) – anoop Mar 29 '17 at 13:14