0

我正在發送一個創建備註的請求。成功回調後,我將新數據推入筆記範圍。但該頁面未顯示更新的筆記數組。 我的代碼如下:在發出請求後顯示數據,而無需在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更新並自動顯示新創建的筆記。但是這沒有發生。任何人都可以讓我知道我在做什麼錯了嗎?

回答

0

Angular會在發生事件(點擊,焦點...)後立即更新視圖。在你的情況下,帖子響應發生在渲染之後,導致過時的視圖。你需要告訴它應用的變化:

var populateNotes = function (err) { 
    ... 
    console.log("notes: ", $scope.notes); 
    $scope.$apply('notes'); 
} 
+0

嗨,我用$適用,但它顯示我這個錯誤: 錯誤:$進展已經消化 –

相關問題