Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: is it possible to invalidate a specific form input field from the controller?

Tags:

angularjs

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'.

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 ?

like image 254
Gal Ben-Haim Avatar asked Jan 23 '13 11:01

Gal Ben-Haim


People also ask

How do you check whether a form is valid or not in AngularJS?

The form instance can optionally be published into the scope using the name attribute. So to check form validity, you can check value of $scope. yourformname. $valid property of scope.

How do you delete a form in AngularJS?

resetForm = function(){ /* reset the data to a new object so that all the properties * of form are reset */ $scope. data = {}; }; });

How do you clear form data after submit in AngularJS?

1) To Remove the values in Form Fields and to reset you can use $setPristine(); $scope. formName. $setPristine();

Which is the valid statement of controller in AngularJS?

ng-controller directive tells AngularJS what controller to use with this view. AngularJS application mainly relies on controllers to control the flow of data in the application.


1 Answers

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).

Then, its possible to display errors with something like 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.

like image 78
Gal Ben-Haim Avatar answered Oct 04 '22 17:10

Gal Ben-Haim