首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度材质提交按钮不起作用

角度材质提交按钮不起作用
EN

Stack Overflow用户
提问于 2019-06-17 11:06:45
回答 2查看 9.4K关注 0票数 3

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

login.html:

代码语言:javascript
复制
<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>

login.component.ts:

代码语言:javascript
复制
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');
  }}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-17 11:11:35

在提交方法调用中错过了(),并且提交按钮在form.之外将其放入form.中它应该如下所示。

TS

代码语言:javascript
复制
(ngSubmit)="Submit()"

HTML

代码语言:javascript
复制
  <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>
票数 2
EN

Stack Overflow用户

发布于 2019-06-17 11:57:03

提交按钮不是表单的一部分

应该是

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56624271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档