如果表达式匹配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'的值,这样可以为你处理剩下的问题? –