这篇文章讲了angular reactive form,

这里是angular file upload 组件 https://malcoded.com/posts/angular-file-upload-component-with-express/

原文:https://malcoded.com/posts/angular-fundamentals-reactive-forms/

------------------------------------

Forms are arguably one point where angular really shines.

So learning how forms work in angular is an essential skill.

In this tutorial we are going to take a close look at one of the two ways to create forms in angular. The reactive forms.

We will dive right into code and discover all the details about reactive forms in the angular framework step by step and with easy examples.

So, without further ado, let’s get started!

Forms in Angular

There are two different approaches to forms in Angular.

The first category are the template-driven forms. Using this method, you first create html-input-elements and then use directives like ngModel to bind their value to a component’s variable.

Personally, I’ve used this technique a lot. But that way only because I did not know about the other category.

Reactive Forms.

Reactive Forms a kind of the opposite to template-driven forms. Instead of defining the form in your template, the structure of the form is defined in code.

This maybe sounds a little bit odd now. At least I felt like this.

“Isn’t it twice the work, to define the form in code?”

“And isn’t it much harder to read?”

That was what I asked myself and that is what you should ask yourself, too!

But please wait with that, until you read this article and fully understand the topic.

To answer these questions, let’s dive right in and just build a small example using reactive forms together!

Importing the Module

For this example we are going to use a blank angular-cli application.

I you haven’t done so already, please go ahead and create one:

ng new [name]

As you probably know, Angular is organized in modules.

To use certain features, we first need to import the modules that contain that feature, before we can use it.

This is true for reactive forms, as well.

To use reactive forms, we need to import the ReactiveFormsModule into our parent module.

In our case, the parent module is the AppModule.

Let’s import the required module into it:

src/app.module.ts

import { BrowserModule } from '@angular/platform-browser'
import { NgModule } from '@angular/core'
import { ReactiveFormsModule } from '@angular/forms' import { AppComponent } from './app.component' @NgModule({
declarations: [AppComponent],
imports: [BrowserModule, ReactiveFormsModule],
providers: [],
bootstrap: [AppComponent],
})
class AppModule {}

Defining a Model

In this example we are going to create a simple contact-form, because I couldn’t think of something more creative.

Before we start creating our form, we first need to define, which information we would like to gather with it.

To do so, we are creating a so called model.

A model is just a class, that contains all the fields, that our data can have.

All we need to do, is to create a new file for that class. Also, I typically create a new folder called models to contain all models of the application.

Inside of that file, we create our model. In this case, the data will be split in two parts.

Personal data about the person using the contact-form and the actual data he/she wants to submit.

The model for that looks like this:

src/models/contact-request.ts

export class ContactRequest {
personalData: PersonalData
requestType: any = ''
text: string = ''
} class PersonalData {
email: string = ''
mobile: string = ''
country: string = ''
}

As you can see, I have also added default values to each field, as we will require them later.

Setting up a Contact-Component

Next, we need a component, to create our form in.

So let’s just create one!

Just use the following command of the angular-cli:

ng generate component contact

This will create a basic component for us to use. It will also import that component into our AppModule automatically.

Creating a Reactive Form in Angular

Now we can finally start creating our form.

Other than with template-driven forms, we can not simply use the model we have created earlier.

Instead we have to create a different one. One, that is only responsible for the form-representation.

But why doe we have to do that? Isn’t that much more complex that just binding to the model ?

Of course it is.

But binding to the model directly comes with one large downside.

We are altering the original data directly.

Reactive Programming and Immutability

This has become bad-practice in the last couple years, as a paradigm called reactive programming took the programming world over more and more.

Why?

Well, the simplest answer might be, that we loose the original data. If the user decides to abort his action, we can not recover the previous state, as we have overwritten it.

What we do with reactive forms instead, is keeping the data in the form model, until the user hits the submit button.

Only then the data is copied over to the original model, replacing everything. This type of overwriting everything is called “immutable objects”.

Form Controls

To create this form-model, angular uses a class called FormGroup.

To define our form-model, we create a new FormGroup. The constructor of this class then takes an object, that can contain sub-form-groups and FormControls.

FormControls represent the leafs in this tree. They are the smallest possible unit and typically represent a HTML-control (input, select, …) of your template.

In actuall code, let’s create a new method, that creates a new instance of the form-model for us. We call this mehtod: createFormGroup.

src/contact/contact.component.ts

createFormGroup() {
return new FormGroup({
personalData: new FormGroup({
email: new FormControl(),
mobile: new FormControl(),
country: new FormControl()
}),
requestType: new FormControl(),
text: new FormControl()
});
}

You will notice the similarities with our contact-request model.

In fact, I would recommend that you always keep them that similar. That way, you wont have any trouble converting the form-model to your actual model.

Because we want the country and the request Type to be select-able as a drop-down, we also have to provide some options to choose from:

src/contact/contact.component.ts

countries = ['USA', 'Germany', 'Italy', 'France']

requestTypes = ['Claim', 'Feedback', 'Help Request']

Finally, we save the result of that method to a public field, to be accessible in our template.

src/contact/contact.component.ts

contactForm: FormGroup;
constructor() {
this.contactForm = this.createFormGroup();
}

Here is how your component should look like now:

src/contact/contact.component.ts

import { Component, OnInit } from '@angular/core'
import { FormControl, FormGroup } from '@angular/forms' @Component({
selector: 'app-contact',
templateUrl: './contact.component.html',
styleUrls: ['./contact.component.css'],
})
class ContactComponent implements OnInit {
contactForm: FormGroup countries = ['USA', 'Germany', 'Italy', 'France'] requestTypes = ['Claim', 'Feedback', 'Help Request'] constructor() {
this.contactForm = this.createFormGroup()
} // Step 1
createFormGroup() {
return new FormGroup({
personalData: new FormGroup({
email: new FormControl(),
mobile: new FormControl(),
country: new FormControl(),
}),
requestType: new FormControl(),
text: new FormControl(),
})
} ngOnInit() {}
}

Implementing the Template

For now that is all we need to do code-wise.

Let’s take a look how the counterpart, the template will look like.

Basically, our HTML does look just like a regular form. The only difference is, that we have to tell angular, which FormGroup and which FormControl to use for each control.

To see the result, I’ve also added a paragraph to print out our form-model as json.

src/contact/contact.component.html

<form [formGroup]="contactForm" (ngSubmit)="onSubmit()" novalidate>
<div formGroupName="personalData" novalidate>
<input formControlName="email" /> <input formControlName="mobile" />
<select formControlName="country">
<option *ngFor="let country of countries" [value]="country"
>{{country}}</option
>
</select>
</div>
<select formControlName="requestType">
<option *ngFor="let requestType of requestTypes" [value]="requestType"
>{{requestType}}</option
>
</select>
<input formControlName="text" />
<button type="submit" [disabled]="contactForm.pristine">Save</button>
<button type="reset" (click)="revert()" [disabled]="contactForm.pristine">
Revert
</button>
</form>

At this point, your app should be in a runable state again.

In case you didn’t know: You can start your application using the

ng serve

command.

Using the Angular Form-Builder

From the example of our TypeScript code above, we can say that the code is starting to look cluttered already.

To solve that, angular provides a service called FormBuilder. This FormBuilder allows us to build our form-model with less code.

To use the FormBuilder, we need to request it via dependency injection. We do so in the constructor of our component.

src/contact/contact.component.ts

constructor(private formBuilder: FormBuilder) {
this.contactForm = this.createFormGroup();
}

The FormBuilder class has to be imported from @angular/forms:

src/contact/contact.component.ts

import { FormControl, FormGroup, FormBuilder } from '@angular/forms'

Now we can use that FromBuilder to build our form-model. For demonstration-purposes, I have created a new method called “createFormGroupWithBuilder”. You can just replace you previous code if you want to.

src/contact/contact.component.ts

createFormGroupWithBuilder(formBuilder: FormBuilder) {
return formBuilder.group({
personalData: formBuilder.group({
email: 'defaul@email.com',
mobile: '',
country: ''
}),
requestType: '',
text: ''
});
}

Using this approach, we have to define the default values of each field. You can just pass them an empty string, or fill it with some default-data as I did with the “email”-field.

Passing a Class to the Form-Builder

I turns out, using the FormBuilder, we can further optimize our code.

Instead of defining our form-model inline, the FormBuilder allows us to pass in any JavaScript object.

So we can just pass in a new instance of our PersonalData class.

src/contact/contact.component.ts

createFormGroupWithBuilderAndModel(formBuilder: FormBuilder) {
return formBuilder.group({
personalData: formBuilder.group(new PersonalData()),
requestType: '',
text: ''
});
}

However, we have to make sure, that all fields of the form are also present in this object. This is why we have assigned the default values in our contact-request-model.

Extracting the Data from the Form

Now that have talked about our more than enough, it’s time to worry about getting our data out of that form into an contact-request object.

For that, we are using the submit-button, we have created earlier in our template. We also already registered the callback-method “onSubmit”.

To be able to listen to the button-press, we need to implement that callback in our component.

Inside of that callback, we can access the form’s values by its values property.

this.contactForm.value

But we need to be careful here. These values are still referenced by our form. If we just copy that reference and modify the data elsewhere, the form will be impacted, as well. This can cause weird side-effects.

This is why we need to create a copy of the data. In this example we are using Object.assign for that.

const result: ContactRequest = Object.assign({}, this.contactForm.value);

However, that is not enough. Object.assign creates something called a shallow copy. That means that all primary-fields of the object are copied properly, but the references to sub-objects or lists are still the same. Not noticing this problem does cause even more weird side-effects.

Instead what we want to do is create a deep copy. We can either achieve that by doing it copying everything manually or use a utility like lodash’s cloneDeep function. We will use the manual method here.

result.personalData = Object.assign({}, result.personalData);

Overall, the onSubmit method should now look something like this:

src/contact/contact.component.ts

onSubmit() {
// Make sure to create a deep copy of the form-model
const result: ContactRequest = Object.assign({}, this.contactForm.value);
result.personalData = Object.assign({}, result.personalData); // Do useful stuff with the gathered data
console.log(result);
}

Resetting the Form

Resetting the form is definitely one of the easier parts with reactive forms.

Again, we are going to use the reset-button, we have included into our template already.

All we need to do now, is to implement the revert-callback into our component.

To reset the form, we can either use the form’s reset method without any parameter,

// Resets to blank object
this.contactForm.reset();

which results in an empty object, or pass a set of default parameters along:

// Resets to provided model
this.contactForm.reset({ personalData: new PersonalData(), requestType: '', text: '' });

All in all the revert method simply looks like this:

src/contact/contact.component.ts

revert() {
// Resets to blank object
this.contactForm.reset(); // Resets to provided model
this.contactForm.reset({ personalData: new PersonalData(), requestType: '', text: '' });
}

Conclusion

In this tutorial we learned how to create forms with angular using the reactive-forms method.

I hope you enjoyed this post.

If you did please hit the share button below and help other people understand reactive-forms in angular, as well.

Have a fantastic day!

angular reactive form的更多相关文章

  1. [Angular] Reactive Form -- FormControl & formControlName, FormGroup, formGroup & formGroupName

    First time dealing with Reactive form might be a little bit hard to understand. I have used Angular- ...

  2. Angular Reactive Form - 填充表单模型

    setValue 使用setValue,可以通过传递其属性与FormGroup后面的表单模型完全匹配的数据对象来一次分配每个表单控件值. 在分配任何表单控件值之前,setValue方法会彻底检查数据对 ...

  3. Angular:Reactive Form的使用方法和自定义验证器

    本文将介绍Angular(Angular2+)中Reactive Form的有关内容,包括: Reactive Form创建方法 如何使用验证 自定义验证器 下面开始进入正文! Reactive Fo ...

  4. [Angular2 Form] Reactive form: valueChanges, update data model only when form is valid

    For each formBuild, formControl, formGroup they all have 'valueChanges' prop, which is an Observable ...

  5. Angular Reactive Forms -- Model-Driven Forms响应式表单

    Angular 4.x 中有两种表单: Template-Driven Forms - 模板驱动式表单 (类似于 AngularJS 1.x 中的表单 )  官方文档:https://v2.angul ...

  6. angular.js form

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. [Angular2 Form] Create and Submit an Angular 2 Form using ngForm

    Forms in Angular 2 are essentially wrappers around inputs that group the input values together into ...

  8. angular实现form验证

    先上效果页面:https://lpdong.github.io/myForm-1/ 其中几个知识点 1.angularJs提供了几个新的type类型: type="password" ...

  9. Angular Reactive Form-响应式表单验证

    内建验证规则 Angular中提供了一些內建的Validators,这些验证规则可以在Template-Driven或Reactive表单中使用. 目前 Angular 支持的内建 validator ...

随机推荐

  1. 【算法】矩阵填数,深度优先搜索(DFS),Pascal改C语言

    面向对象的上机实验 题目 以下列方式向 5*5 矩阵中填入数字.设数字i(1=<i<=25),则数字i+1 的坐标位置应为(E, W).(E, W)可根据下列关系由(x,y)算出: 1)( ...

  2. restful规范与rest_framework

    django两种开发模式: 一.前后端不分离项目 二.前后端分离项目 什么是restful规范? 在前后端不分离的项目中,网页所需要的数据可以直接通过模板渲染的方式传递到前端页面,并且可以很好的支持d ...

  3. mdk3洪水攻击教程

    使得路由器崩溃,直到重启. 1.iwconfig 查看网卡 2.airmon-ng start wlan0 开启网卡监控 3.airodump-ng mon0 查看附近路由信息 4.mdk3 mon0 ...

  4. Java编程思想(二)一切都是对象

    2.1用句柄操纵对象 尽管一切都看作是对象,但是操纵的标识符实际上是指向一个对象的“句柄”(handdle): 拥有一个句柄并不表示必须有一个对象同他连接: String  s:   这里创建的只是句 ...

  5. IDEA插件之FindBugs

    1.是个啥? Findbugs,它是一个静态分析工具,用来查找Java代码中的程序错误.它使用静态分析来识别Java程序中上百种不同类型的潜在错误. 2.安装 File -> Settings ...

  6. Javaweb入门 JDBC第一天

    JDBC的定义和作用 DBC(Java DataBase Connectivity) Java数据库连接, 其实就是利用Java语言/程序连接并访问数据库的一门技术. 之前我们可以通过cmd或者nav ...

  7. win10从零安装eclipse并配置SVN和Maven

    原因:公司的新电脑,重新安装eclipse并通过SVN检入项目,中间经历了各种坎坷,终于在周五配置项目并启动成功:在此记录一下,给后来人以警戒,同时自己也可以常温常新. 刚开始安装的是eclipse4 ...

  8. XML文件介绍

    xml基础详解 1.概述: xml:即可扩展标记语言,xml是互联网数据传输的重要工具,它可以跨越互联网的任何平台,不受编程语言和操作系统的限制,可以说他是一个拥有互联网最高级别通行证的数据携带者.x ...

  9. 5分钟搞定图片鉴黄web应用!

    函数工作流(FunctionGraph,FGS)是一项基于事件驱动的函数托管计算服务,托管函数具备以毫秒级弹性伸缩.免运维.高可靠的方式运行.通过函数工作流,开发者无需配置和管理服务器,只需关注业务逻 ...

  10. 美团CodeM初赛B轮 合并字符串的价值 (线段树,分类讨论)

    输入两个字符串a和b,合并成一个串c,属于a或b的字符在c中顺序保持不变.如"ACG"和"UT"可以被组合成"AUCTG"或"AC ...