简体   繁体   English

Angular2设置 <textarea> 在IE 11上使用占位符文本时显示为脏

[英]Angular2 setting <textarea> as dirty when using placeholder text on IE 11

I did some R & D on this issue, same issue occurred with Angular1 as well and mostly people suggested that use ng-attr-placeholder, but there is no fix for this issue in Angular2. 我对此问题进行了一些研发,Angular1也发生了同样的问题,大多数人建议使用ng-attr-placeholder,但是Angular2中没有针对此问题的修复程序。

I am asking about Angular2 not Angular1. 我问的是Angular2,而不是Angular1。

I have found solution for this: 我已经找到解决方案:

create a custom directive for placeholder text 为占位符文本创建自定义指令

TS: 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);
    }
}

and in your HTML 并在您的HTML中

HTML: HTML:

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

The thing is that it will set <textbox> back to pristine and it is achieved with setTimeOut. 问题是它将<textbox>设置回原始状态,并通过setTimeOut实现。 But for permanent solution angular team should look into it why IE is setting <textbox> to dirty. 但是对于永久解决方案,角度小组应研究IE为什么将<textbox>设置为脏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM