2016-01-13 78 views
1

即使模型得到更新,視圖也不會更新。我搜索了這個問題,我得到了一些解決方案,即使用setTimeout$timeout函數。 我試過上面的函數,但即使使用$超時函數模型得到更新和查看不是。

我是從一個服務設置模型的價值。當一個控制器在服務中設置值時,另一個控制器監聽該服務並更新其模型。

注工廠服務

function loadNoteFactory($http) { 
    var baseURL = "SOME_URL"; 
    var sessionId = "sessionId"; 

    return { 
     getNotes: function() { 
      return $http.get(baseURL+"notes?"+sessionId); 
     }, 
     addNote: function(note) { 
      return $http.post(baseURL+"note?"+sessionId, note); 
     }, 
     editNote: function(tag, tagname) { 
      return $http.put(baseURL +"note/"+ note.id+"?"+sessionId, note); 
     }, 
     deleteTag: function(tagId) { 
      return $http.delete(baseURL +"note/"+ note.id+"?"+sessionId); 
     } 
     }; 
} 

廠服務

function loadSelectedNoteFactory($rootScope){ 

    var selectedNoteFactory = {}; 

    selectedNoteFactory.note = {}; 

    selectedNoteFactory.setCurrentNote = function(note) { 
     this.note = note; 
     $rootScope.$broadcast('noteChanged'); 
    }; 

    return selectedNoteFactory; 
} 

位指示1 - 服務設置新的價值

function loadNoteListControllar($scope, NoteFactory, tagBasedNoteSearchService, selectedNoteFactory){ 

     getUsersNotes();  

     function getUsersNotes(){ 
      NoteFactory.getNotes().success(function(data) { 
       $scope.notes = data.notes; 

       selectedNoteFactory.setCurrentNote($scope.notes[0]); 
      }); 
     } 

    $scope.onSelectNote = function(note){ 
     selectedNoteFactory.setCurrentNote(note); 
    } 
} 

控制器2 - 自我更新的變化,服務

function loadDetailControllar($scope, $timeout, selectedNoteFactory){ 

    $scope.note = {}; 

    $scope.$on('noteChanged', testme); 

    function testme(){ 

     $timeout(function(){ 
      $scope.note = selectedNoteFactory.note; 
     }, 500); 
    } 
} 

的Html

<div class="tagWidget" ng-app="tagWidget"> 
<div class="notelistcontainer floatleft" ng-controller="NoteListController" style="width: 20%; height: 100%;">  
      <div class="notelist" style="border: solid 5px grey;"> 
      <div class="noteitem greyborderbottom" ng-repeat="note in notes"> 
         <div class="paddinglefttwentypx notesubject attachdotdotdot widthhundredpercent" ng-click="onSelectNote(note)">{{::note.subject}}</div> 
        </div> 
       </div>   
      </div> 
      <div class="detailview floatright" ng-controller="DetailController" style="width: 60%; height: 100%;"> 
       <div class="paddinglefttwentypx notetext attachdotdotdot widthhundredpercent">{{::note.notehtmltext}}</div> 
      </div> 
     </div> 
     </div> 

注漿Cotroller,服務和指令

angular.module('tagWidget', []) 
    .factory('tagBasedNoteSearchService', loadTagBasedNoteSearchService) 
    .factory('NoteFactory', loadNoteFactory) 
    .factory('SelectedNoteFactory', loadSelectedNoteFactory) 
    .directive('editableDiv', loadEditableDiv) 
    .directive('toggleIcon', loadToggleIcon) 
    .controller('NoteListController', ['$scope', 'NoteFactory', 'tagBasedNoteSearchService', 'SelectedNoteFactory', loadNoteListControllar]) 
    .controller('DetailController', ['$scope', '$timeout', 'SelectedNoteFactory', loadDetailControllar]) 
'tagBasedNoteSearchService', 'SelectedNoteFactory', loadTagControllar]); 
+0

我們可以看到NoteFactory.getNotes()的代碼嗎?我認爲超出範圍的變量設置的問題可能在那裏。 – bri

+0

@bri添加了NoteFactory服務代碼。 –

+0

你的代碼在哪裏調用,或者說更好的說_injecting_'loadDetailControllar','loadNoteListControllar','loadNoteFactory'等等? –

回答

0

解決方案 - 從EXP刪除:: ression {{::note.notehtmltext}}

相關問題