2

I'm trying to make a simple login form work with an action attribute. The issue here is the request to /login is never sent. Copying and pasting the form in the DOM through the dev tools make the copied one work. Angular seems to prevent the default behavior of the form.

Here is my login form component, very basic:

@Component({
  selector: 'my-app',
  template: `
    <form #f="ngForm" action="/login" method="POST">
      Username: <input type="text"required>
      <br/>
      Password: <input  type="password" required>
      <br/>  
      <button type="submit">Login</button>
    </form>
  `,
  directives: [FORM_DIRECTIVES]
})

I also tried to return true/false in the ngSubmit directive's handler but it does not work too.

Here is a plunker illustrating the issue.

Anyone knows how to tell angular to submit the form with the action request ?

angular.2.0.0-Beta.1

VPusher
  • 376
  • 1
  • 4
  • 11

1 Answers1

11

You can do:

<button type="submit" (click)="f.submit()">Login</button>

or use the ngNoForm attribute in the form tag as @Thierry Templier suggested here:

How to submit form to server in Angular2

Community
  • 1
  • 1
Langley
  • 5,326
  • 2
  • 26
  • 42
  • Indeed, i was using the manual `f.submit()` as workaround but `ngNoForm` is much better ! Thanks. Hope will help. – VPusher Jan 21 '16 at 10:30