Javascript 即使使用$timeout,视图也不会在angularjs中更新
即使模型得到更新,视图也不会更新。我在谷歌上搜索了这个问题,找到了一些解决方案,使用了Javascript 即使使用$timeout,视图也不会在angularjs中更新,javascript,angularjs,Javascript,Angularjs,即使模型得到更新,视图也不会更新。我在谷歌上搜索了这个问题,找到了一些解决方案,使用了setTimeout或$timeout函数。 我尝试了上述函数,但即使使用$timeout函数模型也会得到更新,而视图则不会 我正在从一个服务设置模型的值。当一个控制器在服务中设置值时,另一个控制器侦听该服务并更新其模型 注意工厂服务 function loadNoteFactory($http) { var baseURL = "SOME_URL"; var sessionId = "sess
setTimeout
或$timeout
函数。
我尝试了上述函数,但即使使用$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 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);
}
}
function loadDetailControllar($scope, $timeout, selectedNoteFactory){
$scope.note = {};
$scope.$on('noteChanged', testme);
function testme(){
$timeout(function(){
$scope.note = selectedNoteFactory.note;
}, 500);
}
}
工厂服务
function loadSelectedNoteFactory($rootScope){
var selectedNoteFactory = {};
selectedNoteFactory.note = {};
selectedNoteFactory.setCurrentNote = function(note) {
this.note = note;
$rootScope.$broadcast('noteChanged');
};
return selectedNoteFactory;
}
<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>
控制器1-在服务中设置新值
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 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);
}
}
function loadDetailControllar($scope, $timeout, selectedNoteFactory){
$scope.note = {};
$scope.$on('noteChanged', testme);
function testme(){
$timeout(function(){
$scope.note = selectedNoteFactory.note;
}, 500);
}
}
控制器2-在服务发生变化时更新自身
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 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);
}
}
function loadDetailControllar($scope, $timeout, selectedNoteFactory){
$scope.note = {};
$scope.$on('noteChanged', testme);
function testme(){
$timeout(function(){
$scope.note = selectedNoteFactory.note;
}, 500);
}
}
Html
function loadSelectedNoteFactory($rootScope){
var selectedNoteFactory = {};
selectedNoteFactory.note = {};
selectedNoteFactory.setCurrentNote = function(note) {
this.note = note;
$rootScope.$broadcast('noteChanged');
};
return selectedNoteFactory;
}
<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>
解决方案-从表达式中删除
::
{{::note.notethtmltext}}解决方案-从表达式中删除:
{code>{::note.notethtmltext}}我们可以看到NoteFactory.getNotes()的代码吗?我认为范围外变量设置的问题可能在那里。@bri添加了NoteFactory服务代码。您在代码中调用的位置,或者更好地说是注入loadDetailControllar
,loadNoteListControllar
,loadNoteFactory
,等等?@AhmadBaktashHayeri为注入添加了代码。我们可以看到NoteFactory.getNotes()的代码吗?我认为范围外变量设置的问题可能就在那里。@bri添加了NoteFactory服务代码。您在代码中调用的位置,或者更好地说是注入loadDetailControllar
,loadNoteListControllar
,loadNoteFactory
,等等?@AhmadBaktashHayeri为注入添加了代码。