我还试图在使用ng-model
验证的同时观看minlength
的值。问题是模型值保持空/未定义,直到满足验证标准。
<input ng-model="xyz" minlength="8" />
$scope.$watch('xyz', function(val) {
// Will either be undefined or a
// string bigger than or equal
// to 8 characters.
console.log(val);
});
我知道我只能substring
元素的值,但此代码是在使用$compile
的指令中实现的,所以理想情况下我更喜欢观看模型值
有关如何解决此问题的任何想法?
答案 0 :(得分:3)
https://docs.angularjs.org/api/ng/directive/ngModelOptions
allowInvalid:布尔值,表示可以使用未正确验证的值设置模型,而不是将模型设置为undefined的默认行为。