简体   繁体   中英

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.

I am asking about Angular2 not Angular1.

I have found solution for this:

create a custom directive for placeholder text

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:

<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. But for permanent solution angular team should look into it why IE is setting <textbox> to dirty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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