gpt4 book ai didi

javascript - 异常 : No provider for Angular 2 custom form input

转载 作者:行者123 更新时间:2023-11-30 09:52:03 24 4
gpt4 key购买 nike

我正在尝试实现

ControlValueAccessor

对于自定义 Angular 2 表单输入

我遇到了异常

EXCEPTION: No provider for MyDatePicker! (MyDatePickerValueAccessor -> MyDatePicker)

我将代码上传到 Plunker

Plunker Custom Control Event Binding Working

Plunker Custom Control Value Accessor Not Working

还有我的 ControlValueAccessor 实现

    import {MyDatePicker} from './MyDatePicker';
import { Directive, Provider, forwardRef , } from 'angular2/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from 'angular2/common';
import {CONST_EXPR} from 'angular2/src/facade/lang';

const CUSTOM_VALUE_ACCESSOR = CONST_EXPR(new Provider(
NG_VALUE_ACCESSOR, { useExisting: forwardRef(() => MyDatePickerValueAccessor), multi: false }));

@Directive({
selector: 'my-date-picker',
host: { '(dateChanged)': 'onChange($event)' },
providers: [CUSTOM_VALUE_ACCESSOR]
})
export class MyDatePickerValueAccessor implements ControlValueAccessor {
onChange = (_) => { };
onTouched = () => { };

constructor(private host: MyDatePicker) { }

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

registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
}

这里是我研究的一些链接:

最佳答案

试着写

providers: [CUSTOM_VALUE_ACCESSOR, MyDatePicker]

关于javascript - 异常 : No provider for Angular 2 custom form input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35948989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com