我有一個數組,其中包含模板中卡片的對象以循環播放。如果它檢測到本地數據存儲中沒有任何內容,它將創建一個包含對象的數組。否則,它從本地數據存儲中檢索它。按下一個按鈕會使您看到共享相同控制器的另一個視圖。有一個文本框,如果你按下提交按鈕,它會將它拼接到數組。如果檢測到更改,我也有一個監視功能來更新本地數據存儲。Ng-Repeat在陣列更新後沒有更新
問題是在向數組添加內容後ng-repeat不會更新。如果我只是因爲本地數據存儲加載而刷新頁面,纔會出現這種情況。
任何想法?
代碼:controllers.js
.controller('NotesCtrl', function($scope, localStorageService) {
if (!localStorageService.get("agendaNotes")) {
$scope.notes = [{
title: "Civil War Notes"
}, {
title: "Types of Bacon"
}];
//If it exists retrieve it from keystore.
} else {
$scope.notes = localStorageService.get("agendaNotes");
}
$scope.addNote = function(title) {
$scope.notes.splice(0, 0, {
title: title
})
};
$scope.$watch("notes", function(newVal, oldVal) {
if (newVal !== null && angular.isDefined(newVal) && newVal !== oldVal) {
localStorageService.add("agendaNotes", angular.toJson(newVal));
}
}, true);
})
tab.notes.html
新note.html
<ion-view view-title="New Note">
<ion-content>
<!-- To Do:
- Description
- Images (?)
-->
<form ng-submit="addNote(data.newTitle, data.newDescription); data.newTitle = ''; data.newDescription = ''">
<div class="list">
<div class="list list-inset">
<h3>Note Title:</h1>
<label class="item item-input">
<input type="text" placeholder="Title" ng-model="data.newTitle">
</label>
<h3>Note Description:</h3>
<label class="item item-input">
<input type="text" placeholder="Description" ng-model="data.newDescription">
</label>
</div>
<button class="button button-block button-positive" type="submit">
Add Note
</button>
</div>
</form>
</ion-content>
</ion-view>
我曾嘗試$ $範圍適用(。函數(){})方法,並在控制檯中返回一個錯誤:錯誤:[$ rootScope:inprog] $ apply already apply
任何幫助表示讚賞。謝謝
爲了記錄,建議您替換'$ scope.notes.splice(0,0,{...}) ''scope.notes.unshift({...})'' – matewka
您能否提供plnkr? – geckob