2015-03-30 79 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

相關問題