角材料提交按钮不起作用

时间:2019-06-17 03:06:44

标签: angular angular-material

我使用棱角材料创建登录表单,但是“提交”按钮不起作用,并且控制台中没有出现任何错误。 最初,我尝试通过它发布一个http请求,但是它不起作用,所以我只是用一个简单的日志进行测试,但它仍然不起作用。

login.html:

        <form class="my-form" #loginForm=ngForm (ngSubmit)="Submit()">
          <mat-form-field class="full-width">
            <mat-label>Email</mat-label>
            <input matInput class="form-control"
             [formControl]="emailControl"
             placeholder="Enter Your Nickname" type="email">

            <mat-error *ngIf="emailControl.hasError('email')">

              Please enter a valid email address
            </mat-error>
            <mat-error *ngIf="emailControl.hasError('required')">
              Email is <strong>required</strong>
            </mat-error>
          </mat-form-field>

          <mat-form-field class="full-width">
            <mat-label>Password</mat-label>
            <input [formControl]="passwordControl"
             matInput name="password"
             type="password" 
             class="form-control"
              placeholder="Enter Your  Password">
            <mat-error *ngIf="passwordControl.hasError('required')">
              Password is <strong>required</strong>
            </mat-error>
            <mat-error *ngIf="passwordControl.hasError('minLength')">
              Password should be more then 7 characters
            </mat-error>
          </mat-form-field>
        </form>
      </mat-card-content>
      <mat-card-actions>
        <button mat-raised-button type="submit" color="primary">LOGIN</button>
      </mat-card-actions>
    </mat-card>
    </div>

login.component.ts:

    import { CustomValidators } from '../../custom-validators';
    import { Component, OnInit } from '@angular/core';
    import { FormControl,FormGroup,Validators} from '@angular/forms';
    import { HttpClient } from '@angular/common/http';

    @Component({
      selector: 'login',
      templateUrl: './login.component.html',
      styleUrls: ['./login.component.css']
    })
    export class LoginComponent {
      emailControl = new FormControl('', [Validators.required, Validators.email]);
      passwordControl = new FormControl('', [Validators.required,
        Validators.minLength(8)]);

        constructor(private http :HttpClient) { 

        }
    Submit(){
        console.log('workin');
      }}

2 个答案:

答案 0 :(得分:0)

您错过了 () ,Submit方法调用和Submit按钮位于 form 之外。将其放在 form 中。应该是这样。

TS

(ngSubmit)="Submit()"

HTML

  <form class="my-form" #loginForm=ngForm (ngSubmit)="Submit()">
    ...
     <mat-card-actions>
            <button mat-raised-button type="submit" color="primary">LOGIN</button>
     </mat-card-actions>
    ...
    </form>

答案 1 :(得分:0)

您的提交按钮不属于您的表单

应该是

<form class="my-form" #loginForm=ngForm (ngSubmit)="Submit()">
          <mat-form-field class="full-width">
            <mat-label>Email</mat-label>
            <input matInput class="form-control"
             [formControl]="emailControl"
             placeholder="Enter Your Nickname" type="email">

            <mat-error *ngIf="emailControl.hasError('email')">

              Please enter a valid email address
            </mat-error>
            <mat-error *ngIf="emailControl.hasError('required')">
              Email is <strong>required</strong>
            </mat-error>
          </mat-form-field>

          <mat-form-field class="full-width">
            <mat-label>Password</mat-label>
            <input [formControl]="passwordControl"
             matInput name="password"
             type="password" 
             class="form-control"
              placeholder="Enter Your  Password">
            <mat-error *ngIf="passwordControl.hasError('required')">
              Password is <strong>required</strong>
            </mat-error>
            <mat-error *ngIf="passwordControl.hasError('minLength')">
              Password should be more then 7 characters
            </mat-error>
          </mat-form-field>
      </mat-card-content>
      <mat-card-actions>
        <button mat-raised-button type="submit" color="primary">LOGIN</button>
      </mat-card-actions>
    </mat-card>
</form>