Angular Material 提交按钮不起作用

标签 angular typescript angular-material

我正在使用 Angular Material 创建登录表单,但是提交按钮不起作用,并且我在控制台中没有收到任何错误。 起初我试图通过它发布一个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>

登录.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');
  }}

最佳答案

您在提交方法调用中错过了 () 并且提交按钮在 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>

关于 Angular Material 提交按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56624271/

相关文章:

Angular Material 嵌套拖放

javascript - 响应中的 Angular 2 http 更改值不影响页面

javascript - Angular CLI 和 Angular Universal 无法使用 --prod 在服务器上使 environment.production 为真

javascript - Spectron/WebdriverIO 第 n 个子节点使用 $$[n] 而不是选择器

javascript - 交叉获取不适用于 React Native

javascript - Angular 形按钮定制类型

javascript - Angular 日期选择器发送太多有关日期的数据,需要转换

javascript - Angular 应用程序的参数化

angular - 获取错误 "mat-nav-list is not a known element"

使用formArray进行 Angular 拖放