Javascript 即使在作用域上,视图也不再更新。$apply()
我有一个非常奇怪的怪癖,如果我设置了一个对象属性,视图就会停止更新,对该对象进行任何更改,甚至在范围内 以下是我的指示:Javascript 即使在作用域上,视图也不再更新。$apply(),javascript,angularjs,Javascript,Angularjs,我有一个非常奇怪的怪癖,如果我设置了一个对象属性,视图就会停止更新,对该对象进行任何更改,甚至在范围内 以下是我的指示: .directive('viewEditEntry', function(){ return { link: function(scope, element, attrs){ element.on('click', function(){ scope.$apply(function(){ scope.item.edi
.directive('viewEditEntry', function(){
return {
link: function(scope, element, attrs){
element.on('click', function(){
scope.$apply(function(){
scope.item.editing = true;
scope.item.editValue = angular.copy(scope.item);
})
})
}
}
})
.directive('viewSaveEditEntry', function(){
return {
link: function(scope, element, attrs){
element.on('click', function(){
let self = this;
scope.$apply(function(){
Object.assign(scope.item, scope.item.editValue);
scope.item.saving = true; //If commented out, no issues
setItemInDB(scope.item);
});
function setItemInDB(item){
google.script.run.withSuccessHandler(function(){
//Testing Code
console.log('success'); //Runs
scope.$apply(function(){
console.log('success 2'); //Does not run
item.editing = false;
item.saving = false;
})
}).withFailureHandler(self.failure).client_EditTransaction(item);
};
self.success = function(response){
console.log('success')
scope.$apply(function(){
item.editing = false;
item.saving = false;
})
}
self.failure = function(response){
console.log('success')
scope.$apply(function(){
item.editing = false;
item.saving = false;
})
}
})
}
}
})
项是ng重复中的一个元素
在setItemInDB()
下,范围内的任何代码。$apply()
不会运行。在摘要循环之后,对对象所做的任何更改都不会显示在视图eve中
但是,如果我注释掉scope.item.saving=true
行。没有问题,更改反映在对象上,并且作用域。$apply()
的行为与预期一致
为什么会这样?我在另一个小应用程序中有几乎相同的代码,它似乎没有显示这些问题
AngualrJS1.5.8