我正在尝试使用 Reative Form 创建自定义输入组件,Angular 7
但我无法将事件传递给输入字段。
Mycomponent
有一个标签和一个输入字段以及一个特定的布局。我的component html
样子是这样的:
<div id="customInputGroup" [formGroup]="parentFormGroup" [ngClass]="{'input-value': hasValue()}">
<label [for]="inputId">{{label}}</label>
<input [id]="inputId" [name]="inputName" class="form-control" [placeholder]="placeholder" [formControlName]="inputName" [mask]="mask">
</div>
在我的Component
:
@Component({
selector: 'app-input-field',
templateUrl: './input-field.component.html',
styleUrls: ['./input-field.component.css']
})
export class InputFieldComponent implements OnInit {
@Input('placeholder') placeholder = '' ;
@Input('label') label = '';
@Input('inputId') inputId = '';
@Input('inputName') inputName = '';
@Input('parentFormGroup') parentFormGroup:FormGroup;
@Input('mask') mask;
constructor() { }
ngOnInit() {
}
hasValue(){
return (this.parentFormGroup.get(this.inputName).value != undefined
&& this.parentFormGroup.get(this.inputName).value != null
&& this.parentFormGroup.get(this.inputName).value != '')
}
一切正常,直到我不得不处理一个输入onBlur
事件(或者它可以是任何其他输入事件)。我想要的结果是调用我component
传递这样的任何事件:
<app-input-field (blur)="functionName()"></app-input-field>
或者
<app-input-field (keyup)="functionName()"></app-input-field>
我component
将能够“动态地”将这些事件传递给我的输入字段。有可能做到吗?