Reference angular ng-form from controller

Problem

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

I have

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

This code on the HTML page works fine

 {{myForm.$valid}}

returning true or false as the case may be.

But what I want is to check the status of the form from within a funciton in the controller, for example before posting the data or retrieving data.

I have tried

$scope.myForm.$valid

but this doesn't exist. Neither is there any reference to the myForm object in the $scope itself.

The ng-form is used within an ng-repeat, all of which is within a normal HTML form object which is why it is being used.

As I said, the myForm.$invalid is used to control the display/enabled controls within that form on the HTML page just fine.

Any ideas?

Problem courtesy of: DeclanMcD

Solution

one approach to check if a form is valid upon submitting will be to pass the myForm.$valid into 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 the test function:

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

plnkr

Solution courtesy of: Nitsan Baleli

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.