Angular14自定义表单控件不渲染
创始人
2024-10-22 03:30:27
0

如果你在 Angular 14 中创建了一个自定义表单控件但该控件不能正常渲染,可能是因为你没有正确实现 ControlValueAccessor 接口。以下是一个示例,展示如何正确实现 ControlValueAccessor 接口:

  1. 确保你的表单控件实现了 ControlValueAccessor 接口,并在组件的 providers 中注册。

  2. 在组件类中实现 ControlValueAccessor 接口:

@Component({ selector: 'app-custom-control', templateUrl: './custom-control.component.html', providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => CustomControlComponent), multi: true, }, ], }) export class CustomControlComponent implements ControlValueAccessor { private _value: any; private onChange: (value: any) => void = () => {}; private onTouched: () => void = () => {};

get value(): any { return this._value; }

set value(value: any) { if (value !== this._value) { this._value = value; this.onChange(value); } }

writeValue(value: any): void { this.value = value; }

registerOnChange(fn: (value: any) => void): void { this.onChange = fn; }

registerOnTouched(fn: () => void): void { this.onTouched = fn; }

setDisabledState?(isDisabled: boolean): void { console.log(isDisabled); } }

  1. 在模板文件中,将你的表单控件绑定到 [(ngModel)] 或者使用 Reactive Forms:

  1. 运行程序,你就能够看到你的自定义表单控件正确工作了。

相关内容

热门资讯

透视神器!hhpoker辅助挂... 透视神器!hhpoker辅助挂(透视)hhpoker到底可以作必弊码(原来一直总是有挂)-哔哩哔哩1...
透视神器!模拟器打开hhpok... 透视神器!模拟器打开hhpoker(透视)hhpoker免费透视脚本(切实有透视)-哔哩哔哩1、首先...
透视攻略!hh poker软件... 透视攻略!hh poker软件(透视)hhpoker德州机器人(原来一直总是有透视)-哔哩哔哩1、很...
透视软件!潮汕激k辅助(辅助)... 透视软件!潮汕激k辅助(辅助)新九五辅助微信脚本(其实存在有挂)-哔哩哔哩一、新九五辅助微信脚本可以...
透视脚本!aapoker脚本怎... 透视脚本!aapoker脚本怎么用(透视)aapoker怎么拿好牌(一贯一直都是有透视)-哔哩哔哩1...
透视攻略!hhpoker到底可... 透视攻略!hhpoker到底可以作必弊码(透视)德扑HHpoker有挂吗(一贯一直总是有辅助器)-哔...
透视app!德州透视插件(透视... 透视app!德州透视插件(透视)hhpoker辅助器(真是有透视)-哔哩哔哩1、在德州透视插件插件功...
透视攻略!拱趴大菠萝有挂吗(透... 透视攻略!拱趴大菠萝有挂吗(透视)pokemmo手机版透视脚本(总是是有辅助器)-哔哩哔哩1、不需要...
透视脚本!欢聚水鱼脚本(辅助)... 您好,jj斗地主捕鱼辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多...
透视软件!hhpoker作必弊... 透视软件!hhpoker作必弊实战视频(透视)hhpoker透视脚本下载(其实存在有脚本)-哔哩哔哩...