It it recommeded that when deals with form component, we can create a container component to hold state, and then create a stateless component to enpower the form.

For example:

In the example has two components, one is container component 'meal.component.ts', another is statless component 'meal-form.component.ts'.

For the container component, it talks to service:

import {Component} from '@angular/core';
import {Meal} from '../../../shared/services/meals/meals.service';
@Component({
selector: 'meal',
styleUrls: ['meal.component.scss'],
template: `
<div class="meal">
<div class="meal__title">
<h1>
<img src="/img/food.svg" alt="Food">
<span>Create meal</span>
</h1>
</div>
<div>
<meal-form
(create)="addMeal($event)"
></meal-form>
</div>
</div>
`
})
export class MealComponent {
constructor() { } addMeal(meal: Meal) {
console.log("meal", JSON.stringify(meal, null, 2))
}
}

So 'addMeal' function will dispatch action to talk to the service.

For statless component:

import {ChangeDetectionStrategy, Component, EventEmitter, Output} from '@angular/core';
import {FormBuilder, FormArray, FormGroup, FormControl, Validators} from '@angular/forms';
import {Meal} from '../../../shared/services/meals/meals.service';
@Component({
selector: 'meal-form',
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrls: ['meal-form.component.scss'],
template: `
<div class="meal-form">
<form [formGroup]="form">
<div class="meal-form__name">
<label>
<h3>Meal name</h3>
<input type="text"
formControlName="name"
placeholder="e.g. English Breakfast">
<div class="error" *ngIf="required">
Workout name is required
</div>
</label>
</div> <div class="meal-form__food">
<div class="meal-form__subtitle">
<h3>Food</h3>
<button
type="button"
(click)="addIngredient()"
class="meal-form__add">
<img src="/img/add-white.svg" alt="Add food">
Add food
</button>
</div>
<div formArrayName="ingredients">
<label *ngFor="let c of ingredients.controls; index as i;">
<input type="text" [formControlName]="i" placeholder="e.g Eggs">
<span
class="meal-form__remove"
(click)="removeIngredient(i)"
></span>
</label>
</div>
</div> <div class="meal-form__submit">
<div>
<button type="button" class="button" (click)="createMeal()">
Create Meal
</button>
<a
[routerLink]="['../']"
class="button button--cancel">
Cancel
</a>
</div>
</div>
</form>
</div>
`
})
export class MealFormComponent { @Output()
create = new EventEmitter<Meal>(); form = this.fb.group({
name: ['', Validators.required],
ingredients: this.fb.array([''])
}); get ingredients () {
// Type check for ingredients, mark as FormArray
// Therefore when we use 'ingredients',
// We can get auto complete
return this.form.get('ingredients') as FormArray;
} get required() {
return (
this.form.get('name').hasError('required') &&
this.form.get('name').touched
);
} constructor(private fb: FormBuilder) { } createMeal() {
if (this.form.valid) {
this.create.emit(this.form.value);
}
} addIngredient() {
// Add a new FormControl to FormArray
this.ingredients.push(new FormControl(''));
} removeIngredient(i: number) {
this.ingredients.removeAt(i);
}
}

It uses ReactiveForm to create form.

Things to be notice:

1. Add type check for form array:

  get ingredients () {
// Type check for ingredients, mark as FormArray
// Therefore when we use 'ingredients',
// We can get auto complete
return this.form.get('ingredients') as FormArray;
}

Then whenever you use 'this.ingredients', it will show auto complete.

2. FormArray method:

  addIngredient() {
// Add a new FormControl to FormArray
this.ingredients.push(new FormControl(''));
} removeIngredient(i: number) {
this.ingredients.removeAt(i);
}

最新文章

  1. iOS开源项目周报1215
  2. HTTP协议学习---(二)基本认证
  3. Webpack使用教程一
  4. JodaTime library not available - @DateTimeFormat not supported
  5. 【PHP框架CodeIgniter学习】使用辅助函数—建立自己的JSONHelper
  6. I - Tunnel Warfare - hdu 1540(区间合并更新)
  7. Codeforces 242E:XOR on Segment(位上的线段树)
  8. jsp参数乱码解决
  9. 如何让vue项目兼容IE浏览器
  10. hibernate二级缓存demo2
  11. 17.2 SourceInsight批量注释
  12. Servlet 转发请求与重定向,以及路径问题
  13. SDOI2017 Round1 简要题解
  14. 【Java】 剑指offer(44) 连续子数组的最大和
  15. ROS分布式控制的节点配置
  16. hdu 5774 Where Amazing Happens 水题
  17. 从Objective-C到Swift,你必须会的(一)#pragma mark
  18. 【bzoj4571】美味
  19. mybatis 显示 sql日志
  20. 性能优化小Tips

热门文章

  1. 用Shell脚本过滤Hadoop中不能訪问的节点
  2. 新手学,java使用分水岭算法进行图像切割(一)
  3. jquery08
  4. elasticsearch index 之 Mapping
  5. 移动端meta几个值的设置以及含义
  6. js --- return返回值 闭包
  7. POJ 2457 BFS
  8. centos 6.7下安装rabbitmq 3.6.6过程
  9. 洛谷 P1957 口算练习题
  10. git -处理分支合并