编辑,更容易回答:在表单元素上使用require
或ng-require
属性,如果出现错误,则保留表单$pristine
。
如果需要不想要的东西:
注 - 您需要为$ setPristine()角1.1.x版本的。
假设所有形式的ng-model
的是相同的对象的属性,你可以$watch
对象,遍历属性,看看它们是否undefined
或''
空字符串,$setPristine()
如果他们。
HTML表单 - 所有的模型都是input
对象的属性:
<form name="form">
<input type="text" name="one" ng-model="input.one">
<input type="text" name="two" ng-model="input.two"><br/>
<input type="submit" ng-disabled="form.$pristine">
</form>
在控制器或指令,$监视更改模型,然后通过对象循环,如果所有属性都undefined
或看''
。 (如果在链接功能使用时,您通常会代替$scope
使用scope
。
var setPristine = function(input){
if (input === undefined || input === ''){
return 0;
}
return 1;
}
$scope.$watch('input', function(i){
var flag = 0;
//loop through the model properties
for (var obj in i){
flag +=setPristine(i[obj]);
}
// if nothing in the model object, setPristine()
if(flag===0){
$scope.form.$setPristine();
}
}, true)// true allows $watch of object properties, with some overhead
感谢你!我的问题是 - 在可重用的范例中使用此示例指令不会真正起作用,因为它取决于一个叫做“输入”的模型 - 我有许多模型的'用户','产品'等,所以我希望只是检查任何形式的通用形式元素[] n检查基础模型。是否可以在''元素上使用'$ watch'或者其他等价物? – mmacneil007
当然,我现在正在尝试。 $看一个属性对象有点棘手;)。 – rGil