Kousha
Kousha

Reputation: 36209

Form is undefined in scope in Angular 1.3

I don't if this is something I did, but once I upgraded to Angular 1.3, the form is no longer defined in my scope. My HTML is

<div some-dir>

    <form name="myForm">
    <!-- Form stuff here -->
    <button ng-click="submit();">
    </form>

</div>

The submit function is

scope.submit = function() {
    console.log(scope.myForm);
}

And this returns undefined. I have no ng-if or ng-repeat anywhere. There is only one directive and one scope in this specific directive. Again, this was all working until I upgraded to Angular 1.3

Upvotes: 2

Views: 5307

Answers (3)

Felix
Felix

Reputation: 4595

In my case it happened that my form was nested within another form. So once I switched the <form name="myForm"> to <ng-form name="myForm" ...> the form was defined.

See ng-form docs.

Upvotes: 2

Sergey Narozhny
Sergey Narozhny

Reputation: 1144

The problem can be solved by scoping form data into some scope object:

  <form name="myForms.firstForm">
    <!-- Form stuff here -->
    <button ng-click="submit();">
  </form>

And than:

scope.myForms = {};
scope.submit = function() {
    console.log(scope.myForms.firstForm);
}

Some more info in this comment.

Upvotes: 10

Andrei-
Andrei-

Reputation: 11

Got the same problem and fixed by declaring the controller in the html template that contains the form. So you form declaration would become:

<form name="myForm" ng-controller="SomeController">

New "feature" of 1.3, probably..

Upvotes: 1

Related Questions