[Angular] Implement a custom form component by using control value accessor
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的更多相关文章
- [Angular2 Form] Create custom form component using Control Value Accessor
//switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, ...
- [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. ...
- [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 ...
- Angular 学习笔记 (Custom Accessor + Mat FormField + Custom select)
custom form control 之前就写过了,这里简单写一下. 创建一个组件实现 ControlValueAccessor 接口 @Component({ providers: [ { pro ...
- 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 ...
- UE4 Tutorial - Custom Mesh Component 用于绘制自定义网格的插件CustomMeshComponent
UE4 中用于绘制自定义网格的插件CustomMeshComponent. 转载: UE4 Tutorial - Custom Mesh Component Over the last few w ...
- [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 ...
- [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 ...
- Vue Login Form Component
Vue Login Form Component Account Login <template> <div> <slot></slot> <el ...
随机推荐
- MySQL中好用的GROUP_CONCAT函数
今天看到同事的代码,又学到了一个有用的函数,刚看的时候不太懂,就搜了下用法,看下面这篇文章讲的挺详细就直接转载了,我也写不那么好,呵呵,感谢作者的无私奉献. http://blog.sina.com. ...
- SpringMVC框架的多表查询和增删查改
必须声明本文章==>http://www.cnblogs.com/zhu520/p/7883268.html 一: 1):我的运行环境 我使用myeclipse(你也可以使用eclipse),t ...
- Mybatis mapper.xml文件头文件备份
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- 【转】Flash AS3.0 中的自定义事件
原文 http://www.cnblogs.com/acpp/archive/2010/10/19/1855670.html package { import flash.events.Event; ...
- 出乎意料的else语句
在python中你可能时不时不碰到else语句用在while和for循环中,请不要吃惊,先看看它的作用吧! 实际上在循环语句中,else子句仅仅会在循环完毕后运行.即跳出循环的操作.如break,同一 ...
- HDU - 2254 奥运 (求等比数列和)
Description 北京迎来了第一个奥运会,我们的欢呼声响彻中国大地,所以今年的奥运金牌 day day up! 比尔盖兹坐上鸟巢里,手里摇着小纸扇,看的不亦乐乎,被俺们健儿的顽强拼搏的精神深深的 ...
- vim中使用正則表達式
一.使用正則表達式的命令 使用正則表達式的命令最常见的就是 / (搜索)命令. 其格式例如以下: /正則表達式 还有一个非常实用的命令就是 :s(替换)命令,将第一个//之间的正則表達式替换成第二个/ ...
- ORA-06575: 程序包或函数 NO_VM_DROP_PROC 处于无效状态
SQL> drop user aaa ; drop user aaa ORA-00604: 递归 SQL 级别 1 出现错误 ORA-06575: 程序包或函数 NO_VM_DROP_P ...
- js02---字符串
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- vue 打包成 apk 文件(修改路径)
第一个坑:文件引用路径 现在项目我们什么都没动,是初始化之后直接打包的状态,打开dist/index.htmnl文件整个网页都是一片空白的. 爬坑: 打开 config文件夹/index.js文件 a ...