2015-03-30 78 views
0

我试图根据我在$ digest中为$ scope变量所做的更改来更新我的视图,在对我的变量进行必要的更改后,我调用了$ scope。$ apply(),这实际上导致了一个异常,表示$ scope。$ apply已经在执行中。有人可以帮助我如何解决这个问题。在你的代码

this.showCart = false; 

/*This function is called from the view on a ng-click*/ 
this.addCart=function(){ 

     if (this.showCart === false) { 
      this.showCart = true; 
      this.hideEmptyCart = 'none'; 
      this.showFullCart = 'initial'; 
      this.searchRightMargin = 40; 
      this.updateWidth(); 
      $scope.$apply(); 
     } 
} 
+0

此错误通知您$ scope。$ apply已在进行中。你不需要明确地调用它。你可以发布一些代码吗? – Corey 2015-03-30 21:31:09

+0

感谢Corey的回复,我用一些代码更新了帖子。 – 2015-03-30 21:35:23

+0

你不需要调用'$ scope。$ apply()'。把它拿出来,一切都应该工作。 – 2015-03-30 21:35:34

回答

0

看,你的问题可能是与范围参考。您的this参考范围之外的参考范围仅限于控制器,而函数内部的this参考范围仅限于该功能。试着改变一切$scope

$scope.showCart = false; 

/*This function is called from the view on a ng-click*/ 
$scope.addCart=function(){ 
    if ($scope.showCart === false) { 
     $scope.showCart = true; 
     $scope.hideEmptyCart = 'none'; 
     $scope.showFullCart = 'initial'; 
     $scope.searchRightMargin = 40; 
     $scope.updateWidth(); 
    } 
} 

然后,从ng-click调用你的函数:

<button ng-click="addCart()">Add To Cart</button> 

UPDATE

或者,你可以抱着试试看到控制器的基准为self

var self = this; 
self.showCart = false; 

/*This function is called from the view on a ng-click*/ 
self.addCart=function(){ 
    if (self.showCart === false) { 
     self.showCart = true; 
     self.hideEmptyCart = 'none'; 
     self.showFullCart = 'initial'; 
     self.searchRightMargin = 40; 
     self.updateWidth(); 
    } 
} 
+0

我刚刚通过将this.showCart更改为$ scope.showCart,但现在根据您的建议,我尝试将everthing更改为$ scope,并且它没有帮助:( – 2015-03-30 22:35:47

+0

有可能是另一个问题例如,将'ctrl.addCart()'改为'addCart()'。如果你想保持控制器为''as'',我还包含另一个解决方案。 。 – Corey 2015-03-31 16:23:07

相关问题