T...">

Angular ng-form link from controller

How can I reference an ng-form object from my controller?

I have

<ng-form name="myForm"> </ng-form> 

This HTML page code works fine

  {{myForm.$valid}} 

returns true or false, if possible.

But I want to check the form status from the funciton function in the controller, for example, before sending data or retrieving data.

I tried

 $scope.myForm.$valid 

but this does not exist. There is also no reference to the myForm object in $scope .

ng-form used in ng-repeat , all of which are inside a regular HTML form object, so it is used.

As I said, myForm.$invalid used to control the display / inclusion controls in this form on an HTML page just fine.

Any ideas?

+6
source share
1 answer

one approach to validating the form when submitting will be to pass myForm.$valid to the submit function:

 <ng-form name="myForm" ng-submit="test(myForm.$valid, obj)"> <input type="text" name="test" ng-model="obj.user" required> <input type="submit" ng-click="test(myForm.$valid, obj)" ng-disabled="!myForm.$valid"> </ng-form> 

and test function:

 $scope.test = function($valid, obj) { if (!$valid) return; console.log(obj); } 

plnkr

+2
source

Source: https://habr.com/ru/post/980643/


All Articles