如果表達式匹配regex
,我將self.display
的值更改爲true
。我已將此邏輯寫入$scope.$apply()
塊,但當if
塊被觸發時,我得到Digest already in progress
錯誤。根據控制器中的更新值查看未更新
局部視圖模板 -
<form ng-controller="termsCtrl as terms">
<div ng-show="terms.display">
<input type="checkbox">
<label> Accept </label>
</div>
</form>
控制器 -
if (self.regex.test(self.current)) {
$scope.$apply(function() {
self.display = true;
});
} else {
self.display = false;
}
上述代碼工作。當self.display
爲真時,它會在form
內正確顯示div
,但我得到$digest already in progress
錯誤。
我曾嘗試以下爲止,
1)
$scope.$watch('self.display', function() {
console.debug(self.display);
})
2)
if(!$scope.$$phase) {
$scope.$apply();
}
3)
$timeout(function(){
self.display = true;
});
上述技術不產生任何錯誤,但永遠不會顯示在form
內的。
請問你$範圍是什麼樣子?你對範圍有任何價值嗎? –
,爲什麼你不想在'$ scope'上設置'display'的值,這樣可以爲你處理剩下的問題? –