У меня есть следующая функция внутри контроллера:
app.controller("myCtrl", function(myServ, $scope){
var ctrlScope = this;
ctrlScope.myData = [];
function getData(){
myServ.getData().then(function(data){
$scope.$evalAsync(function(){
ctrlScope.myData = data;
});
});
}
getData(); // myData is initialised properly
ctrlScope.update = function(){
// Data has already changed in backend
getData(); //myData inside controller is updated but ng-repeat is not able to show on the view the updated array
};
});
HTML:
<div data-ng-controller="myCtrl as mc">
<span data-ng-repeat="d in mc.myData">{{d.name}}</span>
<button data-ng-click="mc.update()"> Update </button>
</div>
getData()
инициализирует ng-repeat
правильно, но не может обновить представление, даже если контроллер обновил данные.
Вы меняете ссылку массива. Поэтому данные не обновляются в пользовательском интерфейсе. Вы можете обновить данные, как показано ниже:
function getData(){
myServe.getData().then(function(data){
ctrlScope.myData.length = 0;
ctrlScope.myData.push.apply(ctrlScope.myData, data);
});
}
Добавить
$scope().apply()
ctrlScope.update = function(){
// Data has already changed in backend
getData(); //myData inside controller is updated but ng-repeat is not able to show on the view the updated array
$scope().apply();
};