詳解Angular中實(shí)現(xiàn)自定義組件的雙向綁定的兩種方法
在 Angular 中,對(duì)于表單元素,通過(guò) [(ngModel)] 即可以簡(jiǎn)單地實(shí)現(xiàn)雙向綁定。對(duì)于自定義組件而言,希望實(shí)現(xiàn)同樣的效果可以怎么做呢?
1 實(shí)現(xiàn)自定義組件的 ngModel 指令
如果希望自定義組件能夠具有與表單元素相同的 ngModel 效果,可以通過(guò)在組件內(nèi)實(shí)現(xiàn) ControlValueAccessor 接口達(dá)到目的。
對(duì)于 [(ngModel)] ,需要至少實(shí)現(xiàn)該接口的如下方法:
interface ControlValueAccessor {
writeValue(obj: any): void
registerOnChange(fn: any): void
registerOnTouched(fn: any): void
}
最簡(jiǎn)單的核心實(shí)現(xiàn)示例參考如下。
import { ControlValueAccessor } from '@angular/forms/src/directives';
import { Component, forwardRef, Input } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
@Component({
selector: 'custom-input',
template: `<input [(ngModel)]="value"/>`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => UnionInputComponent),
multi: true
}
]
})
export class CustomInputComponent implements ControlValueAccessor {
constructor() { }
private innerValue: any = '';
private onTouchedCallback: () => void = function () { };
private onChangeCallback: (_: any) => void = function () { };
get value(): any {
return this.innerValue;
}
set value(v: any) {
if (v !== this.innerValue) {
this.innerValue = v;
this.onChangeCallback(v);
}
}
/**
* model view -> view value
*/
writeValue(value: any) {
if (value !== this.innerValue) {
this.innerValue = value;
}
}
/**
* view value ->model value
*/
registerOnChange(fn: any) {
this.onChangeCallback = fn;
}
registerOnTouched(fn: any) {
this.onTouchedCallback = fn;
}
}
2 使用 get/set 關(guān)鍵字實(shí)現(xiàn)父子組件的雙向綁定
其實(shí)實(shí)現(xiàn)雙向綁定內(nèi)部的本質(zhì)原理就是父子組件的事件綁定機(jī)制。簡(jiǎn)單舉例如下。
2.1 自定義子組件定義
import { Input, Output, Component, EventEmitter } from '@angular/core';
@Component({
selector: 'custom-input',
template: `<input [(ngModel)]="innerValue"/>`,
})
export class CustomInputComponent {
innerValue;
@Input()
get twoWayModel() {
return this.innerValue;
}
set twoWayModel(val) {
this.innerValue = val;
this.twoWayModelChange.emit(this.innerValue);
}
@Output() twoWayModelChange: EventEmitter<string> = new EventEmitter</string><string>();
}
2.2 使用自定義組件
在需要使用組件的地方,通過(guò) [(twoWayModel)] 即可實(shí)現(xiàn)雙向綁定的效果。
import { Input, Output } from '@angular/core';
import { Component, forwardRef, Input } from '@angular/core';
@Component({
selector: 'custom-input',
template: `<custom -input [(twoWayModel)]="inputValue" (twoWayModelChange)="onInputValueChange($event)"></custom>`
})
export class abcComponent {
inputValue;
onInputValueChange(val) {
console.log(val);
console.log(val === this.inputValue); // true
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
再談Angular4 臟值檢測(cè)(性能優(yōu)化)
這篇文章主要介紹了再談Angular4 臟值檢測(cè)(性能優(yōu)化),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
仿Angular Bootstrap TimePicker創(chuàng)建分鐘數(shù)-秒數(shù)的輸入控件
這篇文章主要為大家詳細(xì)介紹了仿Angular Bootstrap TimePicker創(chuàng)建分鐘數(shù)-秒數(shù)的輸入控件的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07
Angular6升級(jí)到Angular8報(bào)錯(cuò)問(wèn)題的解決合集
這篇文章主要介紹了Angular6升級(jí)到Angular8報(bào)錯(cuò)問(wèn)題的解決合集,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Angular2學(xué)習(xí)筆記之?dāng)?shù)據(jù)綁定的示例代碼
本篇文章主要介紹了Angular2學(xué)習(xí)筆記之?dāng)?shù)據(jù)綁定的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
利用Ionic2 + angular4實(shí)現(xiàn)一個(gè)地區(qū)選擇組件
ionic是一個(gè)移動(dòng)端開發(fā)框架,使用hybird技術(shù),只要使用前端開發(fā)技術(shù)就可以開發(fā)出電腦端,安卓端和ios端的站點(diǎn)程序。下面這篇文章主要給大家介紹了關(guān)于利用Ionic2 + angular4實(shí)現(xiàn)一個(gè)地區(qū)選擇組件的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07
Angular.js跨controller實(shí)現(xiàn)參數(shù)傳遞的兩種方法
這篇文章主要給大家介紹了關(guān)于Angular.js跨controller實(shí)現(xiàn)參數(shù)傳遞的兩種方法,文中給出了詳細(xì)的介紹和示例代碼,相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-02-02
基于AngularJS實(shí)現(xiàn)的工資計(jì)算器實(shí)例
這篇文章主要介紹了基于AngularJS實(shí)現(xiàn)的工資計(jì)算器,結(jié)合具體實(shí)例形式分析了AngularJS數(shù)值計(jì)算相關(guān)操作技巧,需要的朋友可以參考下2017-06-06

