[英]Angular2 setting <textarea> as dirty when using placeholder text on IE 11
我對此問題進行了一些研發,Angular1也發生了同樣的問題,大多數人建議使用ng-attr-placeholder,但是Angular2中沒有針對此問題的修復程序。
我問的是Angular2,而不是Angular1。
我已經找到解決方案:
為占位符文本創建自定義指令
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中
<textarea type="text" customPlaceholder="Message" class="form-control" formControlName="message" rows="10" >
</textarea>
問題是它將<textbox>
設置回原始狀態,並通過setTimeOut實現。 但是對於永久解決方案,角度小組應研究IE為什么將<textbox>
設置為臟。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.