Home > Web Front-end > JS Tutorial > body text

Angular4 form response function example analysis

小云云
Release: 2017-12-14 11:20:53
Original
1433 people have browsed it

This article mainly introduces the form response function of Angular4 programming, and analyzes the specific implementation steps and related operating techniques of the Angular4 form response function in the form of examples. Friends in need can refer to it. I hope it can help everyone.

Responsive form

1. The responsive form needs to inject the responsive form module into the appmodule file


import { FormsModule, ReactiveFormsModule } from '@angular/forms';
<!--
  这里引用模块的时候要注意,具体是哪个module文件使用了表单,
  因为在某些情况下表单是被appmodule下的某个子module文件使用,
  那么就要在该子module文件中引入响应式表单模块。
-->
@NgModule(
  {imports: [FormsModule, ReactiveFormsModule……]
  ……}
Copy after login


2. Reference

in the form.component.ts component. The first way:


import { Component } from &#39;@angular/core&#39;;
import { FormGroup, FormControl, FormBuilder} from &#39;@angular/forms&#39;;
@Component({
 templateUrl: &#39;forms.component.html&#39;
})
export class FormsComponent {
 formModel: FormGroup;
 constructor(fb: FormBuilder) {
  this.formModel= fb.group({
   formControl1: [&#39;&#39;],
   formControl2: [&#39;&#39;],
   ……
  });
 }
 onSubmit () {
  console.log(this.formModel.value);
 }
}
Copy after login


Second way:


import { Component } from &#39;@angular/core&#39;;
import { FormGroup, FormControl} from &#39;@angular/forms&#39;;
@Component({
 templateUrl: &#39;forms.component.html&#39;
})
export class FormsComponent {
 formModel: FormGroup; /*这里定义表单变量名,HTML文件中绑定时使用*/
 constructor() {
  this.formModel= new FormGroup({
    formControl1: new FormControl(),
    formControl2: new FormControl(),
    ……
  });
 }
 onSubmit () {
  console.log(this.formModel.value);
 }
}
Copy after login


3. Corresponding HTML file


<form action="" method="post" [formGroup]=&#39;formModel&#39;> <!-- 通过指令绑定ts文件中命名的变量名 --!>
  <p class="form-group row">
   <p class="col-md-6">
    <p class="row">
     <label>formControl1</label>
     <input type="text" formControlName=&#39;formControl1&#39;>
    </p>
   </p>
   <p class="col-md-6">
    <p class="row">
     <label>formControl2</label>
    <input type="text" formControlName=&#39;formControl2&#39;>
    </p>
   </p>
  </p>
</form>
Copy after login


##At this point, a simple responsive form is completed.

Related recommendations:

Detailed explanation of the input properties and output properties of Angular4

Project preparation and environment building operations in Angular4

About Angular4.0 data binding matters

The above is the detailed content of Angular4 form response function example analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!