我有一个像这样的输入字段:
<div class="form-group form-group-sm">
<label for="antispam" class="col-sm-2 control-label">1+1+5 = ?
<span class="myForm_error" ng-show="myFormZR.antispam.$error.required">(required field)</span>
<span ng-show="myFormZR.antispam.$dirty && IsMatch">BAD ANSWER</span></label>
<div class="col-sm-10">
<input type="text" class="form-control" name="antispam" placeholder="" required="required" ng-model="myForm.antispam" />
</div>
</div>
在ctrl中:
/* antispam */
var antispamAnswer = "7"
if ($scope.myForm.antispam != antispamAnswer) {
$scope.IsMatch = true;
} else {
$scope.IsMatch = false;
}
它没有工作,提及&#34; BAD ANSWER&#34;总是显示
答案 0 :(得分:1)
当Controller实例化时,您的代码只运行一次。您需要将该代码放在$ watch函数中,用于该ng-model:
$scope.$watch('myForm.antispam', function() {
// that code
})
还要执行大量console.log()
来调试代码,以便了解应用程序中发生了什么以及何时发生。