$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;
$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;
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
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"]; });
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