AngularJS:是否可以使控制器中的特定表单输入字段无效?

时间:2022-12-05 07:58:43

I have a text input field for address, whenever the address is entered and search button next to the input field is clicked, I geocode the address and replace the input text with the result from the geocoder. I also set a scope variable 'addressOk'.

我有一个地址的文本输入字段,每当输入地址并单击输入字段旁边的搜索按钮时,我对地址进行地理编码并将输入文本替换为地理编码器的结果。我还设置了一个范围变量'addressOk'。

is it possible to invalidate this specific form input field according to $scope.addressOk or do I have to write a directive for this address geocoding input that invalidates the form when needed ?

是否可以根据$ scope.addressOk使此特定表单输入字段无效,或者我是否必须为此地址地理编码输入编写指令,以便在需要时使表单无效?

1 个解决方案

#1


35  

I found the solution, add a name attribute to the <form> and <input>, and then in the controller its possible to do $scope.formName.inputName.$setValidity(errorKey, isValid).

我找到了解决方案,在

中添加了一个name属性,然后在控制器中可以执行$ scope.formName.inputName。$ setValidity(errorKey,isValid)。

Then, its possible to display errors with something like ng-show="formName.inputName.$error.errorkey"

然后,它可能显示错误,如ng-show =“formName.inputName。$ error.errorkey”

according to http://docs.angularjs.org/api/ng.directive:form If name attribute is specified, the form controller is published onto the current scope under this name.

根据http://docs.angularjs.org/api/ng.directive:form如果指定了name属性,则表单控制器将以此名称发布到当前作用域。

#1


35  

I found the solution, add a name attribute to the <form> and <input>, and then in the controller its possible to do $scope.formName.inputName.$setValidity(errorKey, isValid).

我找到了解决方案,在

中添加了一个name属性,然后在控制器中可以执行$ scope.formName.inputName。$ setValidity(errorKey,isValid)。

Then, its possible to display errors with something like ng-show="formName.inputName.$error.errorkey"

然后,它可能显示错误,如ng-show =“formName.inputName。$ error.errorkey”

according to http://docs.angularjs.org/api/ng.directive:form If name attribute is specified, the form controller is published onto the current scope under this name.

根据http://docs.angularjs.org/api/ng.directive:form如果指定了name属性,则表单控制器将以此名称发布到当前作用域。