即使在范围内,查看也不再更新。$ apply()

时间:2016-09-22 07:27:15

标签: javascript angularjs

我有一个非常奇怪的怪癖,如果我设置了一个对象属性,那么视图会停止更新,对该对象或甚至在范围内进行任何更改。

以下是我的指示:

.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; 
          })
        }            

      })
    }
  }
})

item是ng-repeat中的一个元素。

setItemInDB()下,scope.$apply()内的任何代码都无法运行。在摘要周期后,对对象所做的任何更改都不会出现在视图前夕。

但是,如果我注释掉scope.item.saving = true行。没有问题,更改反映在对象上,scope.$apply()表现得像预期的那样。

为什么会这样?我在另一个似乎没有显示这些问题的小应用程序中有几乎相同的代码。

AngualrJS 1.5.8

0 个答案:

没有答案
相关问题