角度材料提交按钮不起作用

hou*_*gui 5 typescript angular-material angular

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

登录.html:

<mat-card>
 <mat-card-content>
  <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>
Run Code Online (Sandbox Code Playgroud)

登录.组件.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');
  }}
Run Code Online (Sandbox Code Playgroud)

Sud*_*nda 2

您错过()了 Submit 方法调用,并且提交按钮位于form. 把它放进去form。应该如下。

TS

(ngSubmit)="Submit()"
Run Code Online (Sandbox Code Playgroud)

超文本标记语言

  <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>
Run Code Online (Sandbox Code Playgroud)

  • 是的,现在可以使用了!问题是它超出了表格。谢谢 。 (2认同)