将控件与模板驱动的表单一起使用 angular
Using controls with template-driven forms angular
我正在尝试在 angular 中使用 NGForm,但我不明白的是使用 ngForm 控件
谢谢
堆栈闪电战:https://stackblitz.com/edit/angular-gt66nu
这是一个例子:
app-component.html
<form name="form" #f="ngForm">
<div>
<label>name</label>
<input name="name" [(ngModel)]="model.name">
</div>
<div>
<label>surname</label>
<input name="surname" [(ngModel)]="model.surname">
</div>
</form>
app-component.ts
import { Component, AfterViewInit,ViewChild } from '@angular/core';
import {NgForm} from '@angular/forms'
@Component({
selector: 'my-app',
templateUrl: 'app.component.html'
})
export class AppComponent implements AfterViewInit {
model: any = {};
oldForm;
@ViewChild('f', {static: false}) form: NgForm
onSubmit() {
alert('SUCCESS!! :-)\n\n' + JSON.stringify(this.model))
}
ngAfterViewInit() {
this.oldForm = this.form.controls
console.log('form: ',this.form);
this.form.form.valueChanges.subscribe( item => {
console.log('item: ' ,item);
})
}
}
consol
看看Angular NgForm docs or check out Angular tutorials for Template-driven Forms。
NgForm: directive
Creates a top-level FormGroup instance and binds it to a form to track
aggregate form value and validation status.
简单示例:
import {Component} from '@angular/core';
import {NgForm} from '@angular/forms';
@Component({
selector: 'example-app',
template: `
<form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
<input name="first" ngModel required #first="ngModel">
<input name="last" ngModel>
<button>Submit</button>
</form>
<p>First name value: {{ first.value }}</p>
<p>First name valid: {{ first.valid }}</p>
<p>Form value: {{ f.value | json }}</p>
<p>Form valid: {{ f.valid }}</p>
`,
})
export class SimpleFormComp {
onSubmit(f: NgForm) {
console.log(f.value); // { first: '', last: '' }
console.log(f.valid); // false
}
}
我正在尝试在 angular 中使用 NGForm,但我不明白的是使用 ngForm 控件
谢谢
堆栈闪电战:https://stackblitz.com/edit/angular-gt66nu
这是一个例子:
app-component.html
<form name="form" #f="ngForm">
<div>
<label>name</label>
<input name="name" [(ngModel)]="model.name">
</div>
<div>
<label>surname</label>
<input name="surname" [(ngModel)]="model.surname">
</div>
</form>
app-component.ts
import { Component, AfterViewInit,ViewChild } from '@angular/core';
import {NgForm} from '@angular/forms'
@Component({
selector: 'my-app',
templateUrl: 'app.component.html'
})
export class AppComponent implements AfterViewInit {
model: any = {};
oldForm;
@ViewChild('f', {static: false}) form: NgForm
onSubmit() {
alert('SUCCESS!! :-)\n\n' + JSON.stringify(this.model))
}
ngAfterViewInit() {
this.oldForm = this.form.controls
console.log('form: ',this.form);
this.form.form.valueChanges.subscribe( item => {
console.log('item: ' ,item);
})
}
}
consol
看看Angular NgForm docs or check out Angular tutorials for Template-driven Forms。
NgForm: directive
Creates a top-level FormGroup instance and binds it to a form to track aggregate form value and validation status.
简单示例:
import {Component} from '@angular/core';
import {NgForm} from '@angular/forms';
@Component({
selector: 'example-app',
template: `
<form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
<input name="first" ngModel required #first="ngModel">
<input name="last" ngModel>
<button>Submit</button>
</form>
<p>First name value: {{ first.value }}</p>
<p>First name valid: {{ first.valid }}</p>
<p>Form value: {{ f.value | json }}</p>
<p>Form valid: {{ f.valid }}</p>
`,
})
export class SimpleFormComp {
onSubmit(f: NgForm) {
console.log(f.value); // { first: '', last: '' }
console.log(f.valid); // false
}
}