【发布时间】:2018-07-11 13:43:18
【问题描述】:
我想根据其他表单值“自动填充”输入。 “id”应该是“name”+“.” +“组合”:
<input matInput name="id" required placeholder="ID" #idValue readonly/>
<input matInput ngModel name="name" required placeholder="NAME" autoFillId />
<mat-form-field>
<mat-select ngModel name="combo" required placeholder="COMBO" autoFillId >
<mat-option value=1>Value 1</mat-option>
<mat-option value=2>Value 2</mat-option>
</mat-select>
</mat-form-field>
这是我的指令:
@Directive({
selector: '[autoFillId]'
})
export class AutoFillDirective {
@Output() idValue: EventEmitter<string> = new EventEmitter();
value: string = ".";
@HostListener('input', ['$event']) onInputChange($event) {
this.value = this.value.split(".")[0] + "." + $event.target.value;
this.idValue.emit(this.value);
}
@HostListener('change', ['$event']) onChange($event) {
this.value = $event.value + "." + this.value.split(".")[1];
this.idValue.emit(this.value);
}
}
我的意思是,如果我得到“未定义.2”(如果更改组合)或“myName.undefined”(如果更改输入),它会单独工作。
我怎样才能一起做呢?
【问题讨论】:
-
你在哪里使用
idValue输出? -
我忘记了,已编辑
-
你的模板引用变量
#idValue与@Output是如何连接的?
标签: angular angular5 angular-directive