We have a form component:

          <label>
<h3>Type</h3>
<workout-type
formControlName="type"
></workout-type>
</label> form = this.fb.group({
name: ['', Validators.required],
type: 'strength'
}); constructor(
private fb: FormBuilder
) {}

the 'type' FormControl will be a custom form element component which refers to 'workout-type' componet.

For the workout-type component:

import {ChangeDetectionStrategy, Component, forwardRef} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms'; // Register the control value accessor export const TYPE_CONTROL_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
multi: true,
useExisting: forwardRef(() => WorkoutTypeComponent)
}; @Component({
selector: 'workout-type',
providers: [TYPE_CONTROL_ACCESSOR],
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrls: ['workout-type.component.scss'],
template: `
<div class="workout-type">
<div
*ngFor="let selector of selectors"
[class.active]="selector === value"
(click)="setSelected(selector)"
class="workout-type__pane">
<img src="/img/{{selector}}.svg" alt="{{selector}}">
<p>
{{selector}}
</p>
</div>
</div>
`
})
export class WorkoutTypeComponent implements ControlValueAccessor{ selectors = ['strength', 'endurance'];
private onTouch: Function;
private onModelChange: Function;
private value: string; constructor() { } writeValue(value: string): void {
this.value = value;
} registerOnChange(fn: Function): void {
this.onModelChange = fn;
} registerOnTouched(fn: Function): void {
this.onTouch = fn;
} setSelected(value: string): void {
this.value = value;
this.onModelChange(value);
this.onTouch();
}
}

[Angular] Implement a custom form component by using control value accessor的更多相关文章

  1. [Angular2 Form] Create custom form component using Control Value Accessor

    //switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, ...

  2. [Angular] Adding keyboard events to our control value accessor component

    One of the most important thing when building custom form component is adding accessbility support. ...

  3. [Angular2 Form] Angular 2 Template Driven Form Custom Validator

    In this tutorial we are going to learn how we can also implement custom form field validation in Ang ...

  4. Angular 学习笔记 (Custom Accessor + Mat FormField + Custom select)

    custom form control 之前就写过了,这里简单写一下. 创建一个组件实现 ControlValueAccessor 接口 @Component({ providers: [ { pro ...

  5. How to: Implement a Custom Base Persistent Class 如何:实现自定义持久化基类

    XAF ships with the Business Class Library that contains a number of persistent classes ready for use ...

  6. UE4 Tutorial - Custom Mesh Component 用于绘制自定义网格的插件CustomMeshComponent

    UE4 中用于绘制自定义网格的插件CustomMeshComponent. 转载: UE4 Tutorial - Custom Mesh Component   Over the last few w ...

  7. [MDX] Build a Custom Provider Component for MDX Deck

    MDX Deck is a great library for building slides using Markdown and JSX. Creating a custom Providerco ...

  8. [React] Validate Custom React Component Props with PropTypes

    In this lesson we'll learn about how you can use the prop-types module to validate a custom React co ...

  9. Vue Login Form Component

    Vue Login Form Component Account Login <template> <div> <slot></slot> <el ...

随机推荐

  1. chgrp---改变文件或目录所属的用户组

    chgrp命令用来改变文件或目录所属的用户组.该命令用来改变指定文件所属的用户组.其中,组名可以是用户组的id,也可以是用户组的组名.文件名可以 是由空格分开的要改变属组的文件列表,也可以是由通配符描 ...

  2. 从Java到C++——常量的使用规则

    常量是一种标识符,它的值在执行期间恒定不变.C语言用 #define来定义常量(称为宏常量). C++ 语言除了 #define外还能够用const来定义常量(称为const常量). 一.为什么须要常 ...

  3. 深入理解 GRE tunnel

    深入理解 GRE tunnel 时间 2012-11-08 19:05:22  A Geek's Page 原文  http://wangcong.org/blog/archives/2149 主题  ...

  4. MyBatis自动生成代码之generatorConfig配置文件及其详细解读

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguratio ...

  5. bootsrap中的偏移(栅格系统)

    在最初学习bootsrap这个框架的时候觉得这个框架中的栅格系统是个做自适应很好的工具,而且开发也很方便,是我接触的第一个前端框架,第一次觉得开发如此的简单,今天看到学妹写了一个后台的界面,虽然用到了 ...

  6. 浏览器下管理Linux系统--记webmin的使用

    本文介绍一款浏览器方式来管理linux的一种方式,这款软件就叫webmin,Webmin 让您能够在远程使用支持 HTTPS (SSL 上的 HTTP)协议的 Web 浏览器通过 Web 界面管理您的 ...

  7. spark network-common

    概述 Spark底层使用netty作为节点间通信的桥梁.其实现在common/network-common包中.common/network-common包主要是对netty进行了一层封装,主要是定义 ...

  8. canvas:画布

    画布有默认宽度,如果要自己设置宽带要写在属性上 列: <canvas id = "myCanvas" width = "600" height = &qu ...

  9. STM32上使用JSON

    一.STM32工程中添加JSON 最近在一网2串项目,串口和网口之间可能需要定义一下简单的通信协议,而通信协议上则需要去定义一下通信的数据格式,上次听剑锋说要用Json来定义,目前查了下资料具体如何去 ...

  10. 【习题 8-15 UVA - 1617】Laptop

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 贪心. 把所有的区间按照右端点为第一关键字,左端点为第二关键字升序排. 然后令now = a[i].second. (now即当前的 ...