BenC3
BenC3

Reputation: 716

$scope.form.$error is undefined in Angular controller

I'm trying to iterate through all fields that are required and blank in an Angular controller (it's triggered by a certain button being pressed).

I've got this sitting in the controller:

angular.forEach($scope.form.$error.required, function (field) {
        //Do some things to these fields
    });

From here

The problem is that I get

TypeError: Cannot read property 'required' of undefined

And in Chrome dev tools $scope.form.$error is undefined. I've confirmed that $scope.form is populated as expected.

I think I'm missing something simple, but not sure what it is.

Upvotes: 3

Views: 3531

Answers (1)

Satbir Singh
Satbir Singh

Reputation: 141

I don't know what you are doing with your form, but here is the fiddle. Take a look at it.

  <div ng-controller="MyCtrl">
      <form name="form" novalidate class="simple-form" ng-submit="FormSubmit()">Name:
       <input type="text" ng-model="user.name" required />
       <br />E-mail:
       <input type="email" ng-model="user.email" required/>
       <br />
    <input type="submit" ng-click="FormSubmit()" value="Save" />
       </form>
 </div>

I think you are missing "Form Name". But anyways this fiddle works flawlessly. You can put the "debugger;" and chek the values in angular.forEach() function;

http://jsfiddle.net/Satbir/ztxwd8vs/

Upvotes: 2

Related Questions