11中的问题与占位符Angular 5

时间:2018-04-09 10:04:50

标签: angular internet-explorer-11

我们的项目中存在以下条件

 isSearchFocused ? 'Type two or more characters' : 'Search markets'

in html

  <input class="market-search__search-input"
  type="text"
  #marketSearchInput
  (click)="handleSearchClick()"
   placeholder="{{ isSearchFocused ? 'Type two or more characters' : 'Search markets' }}"
  formControlName="query">

方法'handleSearchClick'

handleSearchClick() {
this.isSearchFocused = true;
this.marketSearchInput.nativeElement.focus();

if (!this.active) {
  this.openModal();
}

}

当值'isSearchFocused'通过事件更改从'false'更改为'true' - 在IE 11以外的所有浏览器中正常工作。在IE 11中,当输入消失焦点时文本更改。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用具有某些CSS样式的表单组和自定义标签来克服此问题。

HTML

<form [formGroup]="myFormGroup">
  <mat-form-field>
    <input matInput type="text"
      #marketSearchInput formControlName="market"
      (focus)="handleSearchClick()" (blur)="handleSearchBlur()">
    <span class="mask-placeholder" 
    *ngIf="this.myFormGroup.value['market'].length === 0">
    {{ isSearchFocused ? 'Type two or more characters' : 'Search markets' }}</span>
  </mat-form-field>
</form>

Ts

import {Component} from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';

/** @title Simple form field */
@Component({
  selector: 'form-field-overview-example',
  templateUrl: 'form-field-overview-example.html',
  styleUrls: ['form-field-overview-example.css']
})
export class FormFieldOverviewExample {
  isSearchFocused: Boolean = false;
  myFormGroup: FormGroup;

  ngOnInit(){
    this.myFormGroup = new FormGroup({
      market: new FormControl('')});
  }
  handleSearchClick(){
    this.isSearchFocused = true;
  }

  handleSearchBlur(){
    this.isSearchFocused = false;
  }
}

CSS / SCSS

.mask-placeholder {
      display: inline-block;
      position: absolute;
      left: 0;
      color: #a9a9a9;
      font-weight: 100;
      top: 6px;
      text-overflow: ellipsis;
      overflow: hidden;
      width: 100%;
      white-space: nowrap;
    }

工作stackblitz link