作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 Angular 5 中创建一个自定义选择输入控件。我有一个实现 SelectControlValueAccessor 的组件。当我实现如下所示的类(class)时。它在代码注释中给出了错误。
import { Component, Input, forwardRef, SimpleChanges, OnChanges } from '@angular/core';
import { SelectControlValueAccessor } from '@angular/forms';
import { SELECT_VALUE_ACCESSOR } from '@angular/forms/src/directives/select_control_value_accessor';
@Component({
selector: 'select-input',
template: ` `,
providers: [
{
provide: SELECT_VALUE_ACCESSOR,
useExisting: forwardRef(() => SelectInputComponent),
multi: true
}
]
})
/*
[ts]
Class 'SelectInputComponent' incorrectly implements class 'SelectControlValueAccessor'. Did you mean to extend 'SelectControlValueAccessor' and inherit its members as a subclass?
Property '_renderer' is missing in type 'SelectInputComponent'.
class SelectInputComponent
*/
export class SelectInputComponent implements SelectControlValueAccessor {
value: any; onChange: (_: any) => void;
onTouched: () => void;
compareWith: (o1: any, o2: any) => boolean;
writeValue(value: any): void {
throw new Error("Method not implemented.");
}
registerOnChange(fn: (value: any) => ): void {
throw new Error("Method not implemented.");
}
registerOnTouched(fn: () => ): void {
throw new Error("Method not implemented.");
}
setDisabledState(isDisabled: boolean): void {
throw new Error("Method not implemented.");
}
}
我的配置如下: typescript :2.5.3 Angular :5.2.11VS 代码:1.24.1
最佳答案
来自文档 https://v6.angular.io/api/forms/SelectControlValueAccessor
SelectControlValueAccessor 是指令而不是接口(interface)
import { Component, OnInit, Output, EventEmitter, Renderer2, ElementRef } from '@angular/core';
import { NG_VALUE_ACCESSOR, SelectControlValueAccessor } from '@angular/forms';
@Component({
selector: 'select-input',
template: ` `,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SelectInputComponent),
multi: true
}
]
})
export class SelectInputComponent extends SelectControlValueAccessor {
value: any;
onChange: (_: any) => void;
onTouched: () => void;
compareWith: (o1: any, o2: any) => boolean;
compareFn(c1: any, c2: any): boolean {
return c1 && c2 ? c1.key === c2.key : c1 === c2;
}
writeValue(value: any): void {
if (value) {
this.value.emit(value); }
}
registerOnChange(fn: (value: any) => any): void {
this.onChange = fn;
}
registerOnTouched(fn: () => any): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
throw new Error('Method not implemented.');
}
constructor(_renderer: Renderer2, _elementRef: ElementRef) {
super(_renderer, _elementRef);
}
}
<select class="form-control" [compareWith]="compareFn" [(ngModel)]="value">
<option value="">Choose a option</option>
<option *ngFor="let option of options" [ngValue]="option">{{option.name}}</option>
</select>
关于Angular 5 SelectControlValueAccessor 实现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51123818/
我想在 Angular 5 中创建一个自定义选择输入控件。我有一个实现 SelectControlValueAccessor 的组件。当我实现如下所示的类(class)时。它在代码注释中给出了错误。
我是一名优秀的程序员,十分优秀!