【发布时间】:2018-07-17 14:57:24
【问题描述】:
我在 angular4 中编写了一个指令,该指令适用于输入字段。 当我在 SPAN 标签上应用它时,它不起作用。 我们可以在 SPAN TAG 上应用指令吗,或者有什么解决方法。
<span dirTemp>45789</span>
dirTemp 实际上将值更正为 45,789
import { Directive, HostListener, ElementRef, OnInit, AfterViewInit, AfterViewChecked, AfterContentChecked } from '@angular/core';
@Directive({
// tslint:disable-next-line:directive-selector
selector: '[dirTemp ]',
})
export class FormatterDirective implements AfterViewChecked {
private el: HTMLInputElement;
constructor(
private elementRef: ElementRef,
private cPipe: MyPipe
) {
this.el = this.elementRef.nativeElement;
}
// ngOnInit() {
// this.el.value = this.cPipe.transform(this.el.value);
// }
ngAfterViewChecked(): void {
this.el.value = this.cPipe.parse(this.el.value);
this.el.value = this.cPipe.transform(this.el.value);
}
@HostListener('focus', ['$event.target.value'])
onFocus(value) {
console.log('in formatter directive:', value);
this.el.value = this.cPipe.parse(value);
}
@HostListener('focusout', ['$event.target.value'])
onFocusout(value) {
this.el.value = this.cPipe.transform(value);
}
}
【问题讨论】:
-
您能否添加一些代码以便我们更好地帮助您?
-
我添加了我的指令,可能我需要添加另一个主机监听器。我将该指令应用于表中的列。 TD 值在 内
-
默认情况下跨度不会触发
focusout。您应该发布您的 html 结构以获取有关您正在尝试做的事情的更多上下文 -
@Pierre,好的。我的html很简单,在一个表中,我有
45789