2017-01-30 68 views
1

我想在應用程序中顯示某個邏輯時顯示按鈕(某個按鈕),然後隱藏此按鈕。

通過單擊某個按鈕,我調用expand()將data.loading設置爲true,此時我想要按鈕出現,當我將data.loading設置爲false時我想隱藏按鈕,但視圖不是更新。

<button ng-show="data.loading">Some Button</button> 
<button ng-click="expand(data)">Other Button</button> 

功能:

$scope.expand = function (data) { 
    data.loading = true; 
    // Some Button must be visible now 
    // Some logic here; 
    data.loading = false; 
    // Some Button must be invisible now 
} 

$範圍$適用() - 返回錯誤:$正在進行

$ scope.safeApply()已經申請 - 不會拋出異常,但不更新視圖。

$ timeout - 不更新視圖。

+0

這裏有什麼'邏輯邏輯部分 - 你在視圖中也有'nng-click'。 – tymeJV

+0

@tymeJV //這裏的邏輯是調用api,nng點擊它的錯字,修復 –

+0

請發表邏輯 - 這可能是整個問題的原因... – tymeJV

回答

2

引用通過$scope屬性數據模型與出當前消化週期的移動你的主要邏輯(與$timeout or $evalAsync)應該在你的情況下,解決「$已申請在進步」的消息一起:

$scope.expand = function() { 
    $scope.data.loading = true; 
    $timeout(function() { 
     // Some logic here; 
     $scope.data.loading = false; 
    }); 
}; 

我通常更喜歡(1)在服務內部保持加載進度,(2)使用承諾來管理進度狀態。在下面的例子中,logicFunction()返回一個承諾。

$scope.expand = function() { 
    progressStatusService.started("Loading something…") 
    logicFunction().finally(function() { 
     progressStatusService.finished("Loading something…") 
    }); 
}; 
// progressStatusService implementation left as a challenge to the reader 

全球進步服務主要是面向應用級負載指示,如果加載狀態隻影響一個特定的部件,那麼它可能是一個開銷。

但是,延期/承諾方法可能通常有用且易於閱讀(請參閱$q上的更多文檔)。

1
$scope.expand = function() { 
    $scope.data.loading = true; 
    // Some Button must be visible now 
    // Some logic here; 
    $scope.data.loading = false; 
    // Some Button must be invisible now 
} 
0

按照@Sreehari的建議使用$scope.data.loading,在您查看時使用ng-if而不是ng-show

相關問題