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?

Community
  • 1
  • 1
Scottie
  • 11,050
  • 19
  • 68
  • 109

1 Answers1

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...

harishr
  • 17,807
  • 9
  • 78
  • 125