角度验证,必需不起作用

前端之家收集整理的这篇文章主要介绍了角度验证,必需不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
最简单的例子永远不起作用.
我使用Angular CLI v1.0.6生成了一个Angular 4应用程序,将app.component.html的内容更改为:

<form #form='ngForm' (ngSubmit)='submitForm(form.value)'>
  <input type='email'
  class='form-control' placeholder='E-mail' name='userEmail' ngModel required >
  <button type='submit'>Submit</button>
</form>

app.component.ts的内容为:

import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css']
})
export class AppComponent {
  submitForm(data){
    console.log(data);
  }
}

我希望在我没有提供电子邮件的情况下不会触发提交功能,但确实如此.
我错过了什么?

P.S.:我已经四处寻找例子(例如:https://scotch.io/tutorials/angular-2-form-validation),但经过几个小时我无法找到解决方案,这就是我来找你的原因.我知道这是在我的脸上,但不知怎的,我看不到它.

解决方法

在使用模板驱动方法时,Angular会自动为表单添加novalidate属性.这就是为什么你不能阻止提交.

您可以使用form.valid来查看整个表单是否有效,然后根据您希望如何处理它来创建自己的逻辑.

猜你在找的Angularjs相关文章