[英]How to make a list update after HTTP post request without refreshing the page [ReactJs]
[英]Showing data after making a post request without refreshing the page in AngularJs
我正在發出創建筆記的請求。 成功回調之后,我將新數據推送到注釋的范圍內。 但是該頁面未顯示更新后的注釋數組。 我的代碼如下:
Controller.js
var populateNotes = function (err) {
$scope.notes = [];
if (err) {
return err;
}
for (var i = 0; i < NotesService.getNotes()
.length; i++) {
$scope.notes.push({
text: NotesService.getNotes()[i].note,
when: $moment(NotesService.getNotes()[
i].ct)
.fromNow()
})
};
console.log("notes: ", $scope.notes);
}
$scope.createNote = function () {
var data = {
note: $scope.notetext
};
model.createNote(id, aid, data, populateNotes);
}
和我的HTML如下:
<tr ng-repeat="note in notes">
<td class="text-center">{{note.text}}</td>
<td class="text-center">{{note.when}}</td>
</tr>
問題在於成功創建郵寄請求后,dom應該會更新並自動顯示新創建的便箋。 但是,這沒有發生。 有人可以讓我知道我在做什么錯嗎?
發生事件(單擊,焦點等)后,Angular將更新視圖。 對於您而言,發布響應會在渲染后發生,從而導致視圖過時。 您需要告訴它應用更改:
var populateNotes = function (err) {
...
console.log("notes: ", $scope.notes);
$scope.$apply('notes');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.