[Angular] Implement a custom form component by using control value accessor
2017-08-18 01:57
761 查看
We have a form component:
the 'type' FormControl will be a custom form element component which refers to 'workout-type' componet.
For the workout-type 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(); } }
相关文章推荐
- [Angular2 Form] Create custom form component using Control Value Accessor
- [Angular] Create a ng-true-value and ng-false-value in Angular by controlValueAccessor
- [Angular] Adding keyboard events to our control value accessor component
- [AngularFire] Angular File Uploads to Firebase Storage with Angular control value accessor
- [Angular] Create custom validators for formControl and formGroup
- 【Angular4】No value accessor for form control with name: '***'
- WinForm中Component Class、User Control及Custom Control的区别和使用
- [Angular] Reactive Form -- FormControl & formControlName, FormGroup, formGroup & formGroupName
- Using encodeURIComponent to avoid errors caused by special chars
- How to Implement Add/Edit/Delete/View with PHP using Angular JS (Part-1)
- [转]WinForm中Component Class、User Control及Custom Control的区别和使用
- Custom xml pattern by using XSD tips
- Using the TDockTabSet component by Jeremy North
- VS中的Form,Control,Component
- [Angular 2] Using a Value from the Store in a Reducer
- NSDictionary里的keysSortedByValueUsingSelector排序使用
- PostBack the form when a value is selected from the results returned by autocomplete extender
- HOW TO ORDER BY A CUSTOM SQL FORMULA/EXPRESSION WHEN USING HIBERNATE CRITERIA API
- [转贴]How to work with stored procedures by using script in an Office InfoPath 2003 form
- 【原创】如何在DataFormWebPart中嵌入自定义控件[How to embeded custom control in DFWP]