显然,我的目标是在使用angular指令之后,使<input type="text" placeholder="example" />元素看起来像<input type="text" [placeholder]="'example'" />
import { Directive, HostBinding } from '@angular/core';
@Directive({
selector: 'input[placeholder]'
})
export class PlaceholderDirective {
@HostBinding('placeholder') placeholder:string;
constructor() {
}
}但我现在真的不知道如何在没有undefined值的情况下在[placeholder]中获取第一个设置的placeholder="A placeholder"。
发布于 2018-02-22 22:17:28
好的,我通过问和写这个问题来回答我自己,但这里是给那些会问自己这个问题的人:我导入了ElementRef来访问原始的占位符值,并直接在constructor中设置它!
import { Directive, HostBinding, ElementRef} from '@angular/core';
@Directive({
selector: 'input[placeholder]'
})
export class PlaceholderDirective{
@HostBinding('placeholder') placeholder:string;
constructor(elementRef: ElementRef) {
this.placeholder = elementRef.nativeElement.placeholder;
}
}https://stackoverflow.com/questions/48929648
复制相似问题