angular - 如何使用 ControlValueAccessor Angular 在自定义输入中使用指令
问题描述
我已经input
在我的 Angular 应用程序使用中创建了一个简单的自定义组件ControlValueAccessor
。所以,当我想创建一个表单input
元素时,我不必调用<input />
,只需调用<my-input>
。
我有一个问题,当我使用时<input />
,我可以使用myDirective
. 例如:
<input type="text" class="form-control" formControlName="name" myDirective />
但是,当我使用 时my-input
,我无法使用myDirective
. 例如:
<my-input formControlName="name" myDirective></my-input>
myDirective在我的输入中不起作用
这是my-input
组件使用ControlValueAccessor
代码:
import { Component, forwardRef } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
@Component({
selector: 'my-input',
templateUrl: './my-input.component.html',
styleUrls: ['./my-input.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => MyInputComponent ),
multi: true
}
]
})
export class MyInputComponent implements ControlValueAccessor {
onChange: () => void;
onTouched: () => void;
value: string;
writeValue(value: string): void {
this.value = value ? value : '';
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
}
更新: myDirective
代码:
import { Directive, HostListener } from '@angular/core';
import { FormControlName } from '@angular/forms';
@Directive({
selector: '[myDirective]'
})
export class MyDirective{
constructor(private formControlName: FormControlName) { }
@HostListener('input', ['$event'])
onInputChange() {
this.formControlName.control.setValue(this.formControlName.value.replace(/[^0-9]/g, ''));
}
}
有没有办法myDirective
在my-input
组件中使用?
提前致谢。
解决方案
你的指令有问题。注入一个NgControl并控制这个 ngControl
export class MyDirective{
constructor(private control: NgControl) { } //<--inject NgControl
@HostListener('input', ['$event'])
onInputChange() {
this.control.control.setValue(this.control.value.replace(/[^0-9]/g, ''));
}
}
你可以在stackblitz中看到
注意:不要忘记包含在模块声明中
@NgModule({
imports: [ BrowserModule, FormsModule,ReactiveFormsModule ],
declarations: [ AppComponent, MyInputComponent,MyDirective ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
推荐阅读
- hyperledger-fabric - composer network start 什么都不返回
- python - Keras ModelCheckpoint vall_loss 减少但表示没有
- c++ - 将对象指针数组初始化为 NULL
- javascript - 如何从图中获取带有 ID 的 SVG?
- php - “试图获得非对象的属性”
- neo4j - Neo4j 浏览器无法完全连接到嵌入式实例
- python - https 获取请求 python (HTTPS / SSL)
- html - 如何停止在实时站点上使用缓存清单
- regex - 从 bash 中的 pom.xml 中解析版本
- python - 使用 python 排列文本文件