This article will take you to understand the forms in angular and learn about the two types of forms: template-driven and model-driven. I hope it will be helpful to everyone!
In Angular, there are two types of forms, namely template driven
and model driven
. [Recommended related tutorials: "angular tutorial"]
1. Template driver
1.1 Overview
The control logic
of the form is written in the component template
, which is suitable for simple
form types.
1.2 Get started quickly
1), introduce the dependent module FormsModule
import { FormsModule } from "@angular/forms" @NgModule({ imports: [FormsModule], }) export class AppModule {}
2), convert the DOM form to ngForm
<form #f="ngForm" (submit)="onSubmit(f)"></form>
3), declare the form field as ngModel
<form #f="ngForm" (submit)="onSubmit(f)"> <input type="text" name="username" ngModel /> <button>提交</button> </form>
4), get the form field value
import { NgForm } from "@angular/forms" export class AppComponent { onSubmit(form: NgForm) { console.log(form.value) // {username: ''} } }
5), form grouping
<form #f="ngForm" (submit)="onSubmit(f)"> <div ngModelGroup="user"> <input type="text" name="username" ngModel /> </div> <div ngModelGroup="contact"> <input type="text" name="phone" ngModel /> </div> <button>提交</button> </form>
import { NgForm } from "@angular/forms" export class AppComponent { onSubmit(form: NgForm) { console.log(form.value) // {contact: {phone: ''}, user:{username: ''}} } }
1.3 Form validation
- ##required Required fields
- minlength Minimum field length
- maxlength Maximum field length
- pattern validation regular example: pattern="\d" matches a value
<form #f="ngForm" (submit)="onSubmit(f)"> <input type="text" name="username" ngModel required pattern="\d" /> <button>提交</button> </form>
export class AppComponent { onSubmit(form: NgForm) { // 查看表单整体是否验证通过 console.log(form.valid) } }
<!-- 表单整体未通过验证时禁用提交表单 --> <button type="submit" [disabled]="f.invalid">提交</button>
<form #f="ngForm" (submit)="onSubmit(f)"> <input #username="ngModel" /> <div *ngIf="username.touched && !username.valid && username.errors"> <div *ngIf="username.errors.required">请填写用户名</div> <div *ngIf="username.errors.pattern">不符合正则规则</div> </div> </form>Specify the style when the form item fails validation.
input.ng-touched.ng-invalid { border: 2px solid red; }
2. Model driven
2.1 Overview
## of the form #Control logicWritten in the component class
, it has more control over the verification logic and is suitable for complex
form types. In a model-driven form, the form field needs to be an instance of the
class. The instance object can verify the value in the form field
, whether the value has been modified, etc.
A group of form fields constitutes the entire form. The entire form needs to be an instance of the FormGroup
class, which can overall
validate the form.
- FormControl: a form item in the form group
- FormGroup: form group, the form is at least A FormGroup
- FormArray: used for complex forms, which can dynamically add form items or form groups. During form validation, if one item in the FormArray fails, the entire form fails.
2.2 Get started quickly1), introduce ReactiveFormsModule
import { ReactiveFormsModule } from "@angular/forms" @NgModule({ imports: [ReactiveFormsModule] }) export class AppModule {}
2), in the component Create a FormGroup form control object in the class
import { FormControl, FormGroup } from "@angular/forms" export class AppComponent { contactForm: FormGroup = new FormGroup({ name: new FormControl(), phone: new FormControl() }) }
3), associate the form in the component template
<form [formGroup]="contactForm" (submit)="onSubmit()"> <input type="text" formControlName="name" /> <input type="text" formControlName="phone" /> <button>提交</button> </form>
4), get the form value
export class AppComponent { onSubmit() { console.log(this.contactForm.value) } }
5), and set the form default value
contactForm: FormGroup = new FormGroup({ name: new FormControl("默认值"), phone: new FormControl(15888888888) })
6), form grouping
contactForm: FormGroup = new FormGroup({ fullName: new FormGroup({ firstName: new FormControl(), lastName: new FormControl() }), phone: new FormControl() })
<form [formGroup]="contactForm" (submit)="onSubmit()"> <div formGroupName="fullName"> <input type="text" formControlName="firstName" /> <input type="text" formControlName="lastName" /> </div> <input type="text" formControlName="phone" /> <button>提交</button> </form>
onSubmit() { console.log(this.contactForm.value.name.username) console.log(this.contactForm.get(["name", "username"])?.value) }
2.3 FormArrayRequirement: Display a set of contact information on the page by default , you can add more contact groups by clicking the button.
import { Component, OnInit } from "@angular/core" import { FormArray, FormControl, FormGroup } from "@angular/forms" @Component({ selector: "app-root", templateUrl: "./app.component.html", styles: [] }) export class AppComponent implements OnInit { // 表单 contactForm: FormGroup = new FormGroup({ contacts: new FormArray([]) }) get contacts() { return this.contactForm.get("contacts") as FormArray } // 添加联系方式 addContact() { // 联系方式 const myContact: FormGroup = new FormGroup({ name: new FormControl(), address: new FormControl(), phone: new FormControl() }) // 向联系方式数组中添加联系方式 this.contacts.push(myContact) } // 删除联系方式 removeContact(i: number) { this.contacts.removeAt(i) } ngOnInit() { // 添加默认的联系方式 this.addContact() } onSubmit() { console.log(this.contactForm.value) } }
<form [formGroup]="contactForm" (submit)="onSubmit()"> <div formArrayName="contacts"> <div *ngFor="let contact of contacts.controls; let i = index" [formGroupName]="i" > <input type="text" formControlName="name" /> <input type="text" formControlName="address" /> <input type="text" formControlName="phone" /> <button (click)="removeContact(i)">删除联系方式</button> </div> </div> <button (click)="addContact()">添加联系方式</button> <button>提交</button> </form>
2.4 Built-in form validator1), use the validation rules provided by the built-in validator to validate the form fields
import { FormControl, FormGroup, Validators } from "@angular/forms" contactForm: FormGroup = new FormGroup({ name: new FormControl("默认值", [ Validators.required, Validators.minLength(2) ]) })
2), Get whether the entire form has passed the verification
onSubmit() { console.log(this.contactForm.valid) }
<!-- 表单整体未验证通过时禁用表单按钮 --> <button [disabled]="contactForm.invalid">提交</button>
3), and display the error message when the verification has passed in the component template
get name() { return this.contactForm.get("name")! }
<form [formGroup]="contactForm" (submit)="onSubmit()"> <input type="text" formControlName="name" /> <div *ngIf="name.touched && name.invalid && name.errors"> <div *ngIf="name.errors.required">请填写姓名</div> <div *ngIf="name.errors.maxlength"> 姓名长度不能大于 {{ name.errors.maxlength.requiredLength }} 实际填写长度为 {{ name.errors.maxlength.actualLength }} </div> </div> </form>
2.5 Customize the synchronization form Validator
- The type of custom validator is TypeScript class
- The class contains specific verification methods, verification The method must be a static method
- The verification method has a parameter control, of type AbstractControl. In fact, it is the type of the instance object of the FormControl class.
- If the verification is successful, null will be returned.
- If the verification fails, the object and the properties in the object will be returned. It is the verification identifier, and the value is true, indicating that the verification failed.
- The return value of the verification method is ValidationErrors | null
-
import { AbstractControl, ValidationErrors } from "@angular/forms" export class NameValidators { // 字段值中不能包含空格 static cannotContainSpace(control: AbstractControl): ValidationErrors | null { // 验证未通过 if (/\s/.test(control.value)) return { cannotContainSpace: true } // 验证通过 return null } }
import { NameValidators } from "./Name.validators" contactForm: FormGroup = new FormGroup({ name: new FormControl("", [ Validators.required, NameValidators.cannotContainSpace ]) })
<div *ngIf="name.touched && name.invalid && name.errors"> <div *ngIf="name.errors.cannotContainSpace">姓名中不能包含空格</div> </div>
2.6 Custom asynchronous form validatorimport { AbstractControl, ValidationErrors } from "@angular/forms"
import { Observable } from "rxjs"
export class NameValidators {
static shouldBeUnique(control: AbstractControl): Promise<ValidationErrors | null> {
return new Promise(resolve => {
if (control.value == "admin") {
resolve({ shouldBeUnique: true })
} else {
resolve(null)
}
})
}
}
contactForm: FormGroup = new FormGroup({
name: new FormControl(
"",
[
Validators.required
],
NameValidators.shouldBeUnique
)
})
<div *ngIf="name.touched && name.invalid && name.errors">
<div *ngIf="name.errors.shouldBeUnique">用户名重复</div>
</div>
<div *ngIf="name.pending">正在检测姓名是否重复</div>
2.7 FormBuilderCreate the form
A shortcut.- this.fb.control
- : Form item
- : Form group, the form is at least one FormGroup
- : used for complex forms, you can dynamically add form items or form groups during form validation , one item in FormArray failed, and the whole failed.
import { FormBuilder, FormGroup, Validators } from "@angular/forms" export class AppComponent { contactForm: FormGroup constructor(private fb: FormBuilder) { this.contactForm = this.fb.group({ fullName: this.fb.group({ firstName: ["", [Validators.required]], lastName: [""] }), phone: [] }) } }
实际工作中,我们常常需要根据某个表单值得变化而进行相应的处理,一般可以使用 2.8.1 ngModalChange angular官方并不建议使用ngModalChange。 2.8.2 表单控制 2.9 练习 1)、获取一组复选框中选中的值 2)、获取单选框中选中的值 2.10 其他 patchValue:设置表单控件的值(可以设置全部,也可以设置其中某一个,其他不受影响) setValue:设置表单控件的值 (设置全部,不能排除任何一个) valueChanges:当表单控件的值发生变化时被触发的事件 reset:表单内容置空 更多编程相关知识,请访问:编程视频!!ngModalChange
或者表单来实现<div>
<input type="text" [(ngModal)]="name" (ngModalChange)="nameChange()" />
</div>
import { FormControl, FormGroup } from "@angular/forms"
export class AppComponent {
public name = 'a';
public nameChange() {
}
}
<div [formGroup]="contactForm">
<input type="text" formControlName="name" />
</div>
import { FormControl, FormGroup } from "@angular/forms"
export class AppComponent {
contactForm: FormGroup = new FormGroup({
name: new FormControl()
})
ngOnInt() {
this.contactForm.get("name").valueChanges.subscribe(data => {
console.log(data);
}
}
}
<form [formGroup]="form" (submit)="onSubmit()">
<label *ngFor="let item of Data">
<input type="checkbox" [value]="item.value" (change)="onChange($event)" />
{{ item.name }}
</label>
<button>提交</button>
</form>
import { Component } from "@angular/core"
import { FormArray, FormBuilder, FormGroup } from "@angular/forms"
interface Data {
name: string
value: string
}
@Component({
selector: "app-checkbox",
templateUrl: "./checkbox.component.html",
styles: []
})
export class CheckboxComponent {
Data: Array<Data> = [
{ name: "Pear", value: "pear" },
{ name: "Plum", value: "plum" },
{ name: "Kiwi", value: "kiwi" },
{ name: "Apple", value: "apple" },
{ name: "Lime", value: "lime" }
]
form: FormGroup
constructor(private fb: FormBuilder) {
this.form = this.fb.group({
checkArray: this.fb.array([])
})
}
onChange(event: Event) {
const target = event.target as HTMLInputElement
const checked = target.checked
const value = target.value
const checkArray = this.form.get("checkArray") as FormArray
if (checked) {
checkArray.push(this.fb.control(value))
} else {
const index = checkArray.controls.findIndex(
control => control.value === value
)
checkArray.removeAt(index)
}
}
onSubmit() {
console.log(this.form.value)
}
}
export class AppComponent {
form: FormGroup
constructor(public fb: FormBuilder) {
this.form = this.fb.group({ gender: "" })
}
onSubmit() {
console.log(this.form.value)
}
}
<form [formGroup]="form" (submit)="onSubmit()">
<input type="radio" value="male" formControlName="gender" /> Male
<input type="radio" value="female" formControlName="gender" /> Female
<button type="submit">Submit</button>
</form>
The above is the detailed content of Angular learning talks about two types of forms. For more information, please follow other related articles on the PHP Chinese website!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool