3

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.

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this pointed me in the right direction. I actually did have the form named but the code was saving the object itself to $scope.form which was overriding the actual form object. I untangled them and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.