angular 6 中带有 select 框的 ngForm 简单示例
ngForm simple example in angular 6 with select box
Angular 6个表单验证的简单例子。使用电子邮件验证和 select 框(下拉).ngForm angular 6 中带有 select 框
的简单示例
使用 ngForm。将其导入您的 component.module.ts(模块文件)
import { FormsModule } from '@angular/forms';
将它添加到您的组件模板
<form role="form" (ngSubmit)="f.form.valid && onSubmit()" #f="ngForm" novalidate>
<select name="name_id" [(ngModel)]="model.name_id" #name_id="ngModel" [ngClass]="{ 'is-invalid': f.submitted && name_id.invalid }" required>
<option value="" disabled selected>Select Name</option>
<option *ngFor="let name of allnames" [value]="name.name_id">{{name.name}}</option>
</select>
<div *ngIf="f.submitted && name_id.invalid" class="invalid-feedback">
<div *ngIf="name_id.errors.required">Name is required</div>
</div>
<input type="email" placeholder="Email" name="email" [(ngModel)]="recoverModel.email" #email="ngModel" pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$" [ngClass]="{ 'is-invalid': recoverForm.submitted && email.invalid }" required />
<div *ngIf="recoverForm.submitted && email.invalid" class="invalid-feedback">
<div *ngIf="email.errors.required">Email is required</div>
<div *ngIf="email.errors.pattern">Email is invalid</div>
</div>
<button type="submit">save</button>
</form>
添加你的component.ts
allnames = [{
name_id: 1,
name: 'Jhon'
}, {
name_id: 2,
name: 'Chena'
}, {
name_id: 3,
name: 'Jack'
}]
model: any = {};
ngOnInit() {
this.model.name_id = 2;
}
姓名 Chena selected 在 select 框和电子邮件有效。它工作得很好。我会用它。
Angular 6个表单验证的简单例子。使用电子邮件验证和 select 框(下拉).ngForm angular 6 中带有 select 框
的简单示例使用 ngForm。将其导入您的 component.module.ts(模块文件)
import { FormsModule } from '@angular/forms';
将它添加到您的组件模板
<form role="form" (ngSubmit)="f.form.valid && onSubmit()" #f="ngForm" novalidate>
<select name="name_id" [(ngModel)]="model.name_id" #name_id="ngModel" [ngClass]="{ 'is-invalid': f.submitted && name_id.invalid }" required>
<option value="" disabled selected>Select Name</option>
<option *ngFor="let name of allnames" [value]="name.name_id">{{name.name}}</option>
</select>
<div *ngIf="f.submitted && name_id.invalid" class="invalid-feedback">
<div *ngIf="name_id.errors.required">Name is required</div>
</div>
<input type="email" placeholder="Email" name="email" [(ngModel)]="recoverModel.email" #email="ngModel" pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$" [ngClass]="{ 'is-invalid': recoverForm.submitted && email.invalid }" required />
<div *ngIf="recoverForm.submitted && email.invalid" class="invalid-feedback">
<div *ngIf="email.errors.required">Email is required</div>
<div *ngIf="email.errors.pattern">Email is invalid</div>
</div>
<button type="submit">save</button>
</form>
添加你的component.ts
allnames = [{
name_id: 1,
name: 'Jhon'
}, {
name_id: 2,
name: 'Chena'
}, {
name_id: 3,
name: 'Jack'
}]
model: any = {};
ngOnInit() {
this.model.name_id = 2;
}
姓名 Chena selected 在 select 框和电子邮件有效。它工作得很好。我会用它。