我做了一些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>肮脏的.