我一直没有使用未指定名称属性的表单控件的值访问器"
有些人说应该添加ngDefaultControl,但它不会触发我的组件中的任何更改
我尝试实现ControlValueAccessor,但我回到上面的错误消息
import { Component, OnInit,Input,OnChanges,SimpleChange } from '@angular/core';
import { ControlValueAccessor } from "@angular/forms";
import { ProductsService } from '../../pages/products/products.service';
@Component({
selector: 'my-product-input',
templateUrl: './productInput.component.html',
styleUrls: ['./productInput.component.css'],
})
export class ProductInputComponent implements OnInit,OnChanges,ControlValueAccessor {
onChange;
@Input('productID') productID:number=null;
private product:any;
constructor(
private productsService: ProductsService,
) {}
ngOnInit() {
this.getProduct();
}
getProduct() ...
ngOnChanges(changes: { [propName: string]: SimpleChange }) {
... never gets triggered
}
writeValue( value : any ) : void {
this.productID=value;
this.getProduct();
}
registerOnChange( fn : any ) : void {
this.onChange = fn;
}
// not sure about this entry
change( $event ) {
this.onChange($event.target.textContent);
}
setDisabledState( _isDisabled : boolean ) : void {
}
registerOnTouched(_fn: any):void
{
}
}
我做错了什么?我找不到最近实施的任何例子
感谢
答案 0 :(得分:0)
您需要通过创建具有NG_VALUE_ACCESSOR
功能的注入令牌并将其添加到组件的提供者来告诉注入器这是一个ControlValueAcessor:
export const MY_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => ProductInputComponent),
multi: true
};
@Component({
selector: 'my-product-input',
templateUrl: './productInput.component.html',
styleUrls: ['./productInput.component.css'],
providers: [MY_VALUE_ACCESSOR]
})
...
答案 1 :(得分:0)
太快了,不能在这里发帖
无论如何,我找到了正确的教程享受;-)