我正在尝试从控制器验证angularjs表单,如下所示:
我的表格:
<form name="appGroupForm" class="form-horizontal">
<input id="unique-name" ng-model="appGroup.name" name="name" type="text"
placeholder="New App Group" class="form-control input-md"
ng-trim="false" ng-pattern="/[^\s]+/"
ng-change="uniqueAppGroupName()"
required />
</form>
控制器:
$scope.uniqueAppGroupName = function () {
$scope.appGroupForm.name.$setValidity('unique', true);
};
但它会引发错误:
TypeError: Cannot read property '$setValidity' of undefined
还试过:
$scope.appGroupForm.name.$setValidity.unique = !error;
但仍然没有成功,任何想法&amp;这个原因的建议?
答案 0 :(得分:0)
尝试$scope.appGroupForm.unique-name.$setValidity('unique-name', true);
编辑:
$scope.appGroupForm[name].$setValidity('unique-name', true);