在IE 11上使用占位符文本时,Angular2设置为脏

前端之家收集整理的这篇文章主要介绍了在IE 11上使用占位符文本时,Angular2设置为脏前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我做了一些R&关于这个问题,同样的问题也发生在Angular1上,大多数人建议使用ng-attr-placeholder,但Angular2中没有解决这个问题.

我问的是Angular2而不是Angular1.

解决方法

我找到了解决方案:

为占位符文本创建自定义指令

TS:

import { Directive,ElementRef,Input } from '@angular/core';
import { NgControl } from "@angular/forms";
@Directive({
  selector: '[customPlaceholder]'
})
export class PlaceholderDirective {
  constructor(private el: ElementRef,private control : NgControl) { }
  @Input('customPlaceholder')
    public set defineInputType(pattern: string) {
        this.el.nativeElement.placeholder = pattern;
        setTimeout(() => {
            this.control.control.markAsPristine();
        },0);
    }
}

并在您的HTML中

HTML:

<textarea type="text" customPlaceholder="Message" class="form-control" formControlName="message" rows="10" >
</textarea>

问题是,它将设置< textBox>回到原始状态,用setTimeOut实现.但对于永久解决方案,角度团队应该调查它为什么IE设置< textBox>肮脏的.

猜你在找的Angularjs相关文章