0

I have been following this AngularJS - Server side validation and client side forms SO question, but I can't seem to get it to work.

Given the following code:

<div ng-controller='LoginCtrl'>
    <form id="myForm" onsubmit="return false;" novalidate>
        <input type="text" ng-model="model.userName" />
        <button ng-click="login()">Login</button>
    </form>
</div>

and

app.controller('LoginCtrl', ['$scope', function ($scope) {
    $scope.model = { userName: '' };

    $scope.login = function () {
        console.log($scope.myForm);  // <-- this is undefined
    };
}]);

The console.log shows undefined. I thought that by including the form as a child of the controller div, angular automatically included the form as a variable in my scope.

What am I doing wrong here?

2
  • 1
    Change your form tag, form name="myform". Angular validation works with form name, not ,id Commented Jul 23, 2014 at 15:18
  • That worked! Set as the answer and I'll accept it. Commented Jul 23, 2014 at 15:21

1 Answer 1

2

change the form tag to

<form name="myForm" onsubmit="return false;" novalidate>

few more points

  1. try not to use id's when working with angular, this will help you get away from jquery

  2. us ng-form, instead of form tag, then you can have child forms, each with its own validation state...

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

Comments

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.