6

I tried to build my first login form with Angular 4.0.0 with Material 2. But the Form won't submit and trigger the function.

<form #UserLogin="ngForm" *ngIf="active" (ngSubmit)="onSubmit(UserLogin.value)">
<md-input-container>
    <input mdInput [(ngModel)]="data.email" ngControl="email" name="email" placeholder="Benutzername" type="text" required>
</md-input-container>

<md-input-container>
    <input mdInput [(ngModel)]="data.password" ngControl="password" name="password" placeholder="Passwort" type="password" required>
</md-input-container>

<button md-button class="submit-btn" type="submit" [disabled]="!UserLogin.form.valid">Login!</button>

The Submit function:

onSubmit(value: any) {

    console.log('sdfdfg');

    Object.assign(value, this.additionalData);
    this.submitted = true;

    this.auth.login(value).subscribe(
        data => {
            this.loginSuccess.emit(data);
        },
        error => {
            for (const field in this.formErrors) {
                if (this.formErrors.hasOwnProperty(field)) {

                    this.formErrors[field] = [];
                    if (this.validationMessages[field].hasOwnProperty(error.systemCode)) {
                        this.formErrors[field].push(this.validationMessages[field][error.systemCode]);
                    }
                }
            }
        }
    );

}

When i click the login button, he does not print the console log into the console. Any idea why?

1 Answer 1

4

change button type="button"

<form #UserLogin="ngForm" (ngSubmit)="onSubmit(UserLogin)">
<md-input-container>
    <input mdInput [(ngModel)]="data.email" name="email" placeholder="Benutzername" class="form-control"  type="text" required>
</md-input-container>

<md-input-container>
    <input mdInput [(ngModel)]="data.password" name="password" class="form-control" placeholder="Passwort" type="password" required>
</md-input-container>

<button md-button class="submit-btn" type="button" [disabled]="!UserLogin.form.valid">Login!</button>
</form>

onSubmit(form: ngForm) {

console.log('sdfdfg');
console.log(form.value);

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

3 Comments

Thanks, i changed it to type="button", but he is still not triggering the console.log('sdfdfg');
hmmm thanks, but still no console log. maybe they change any synt ax in latest angular version?
It working for me but in console its not showing form data

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.