Search code examples
angularangular-materialinternet-explorer-11placeholdermat-input

input event of matInput is called on page load in Internet Explorer 11, when a placeholder is set


I found out that there is a IE bug, where setting the placeholder is calling the input event as described here. This happens on page load, so there was no user interaction.

My code:

app.component.html

<mat-form-field>
  <input matInput placeholder="test" (input)="onValueChange('test')">
</mat-form-field>

app.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent implements OnInit {

  ngOnInit() {
  }

  onValueChange(value: string) {
    console.log("should not be in here");
  }
}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatDialogModule } from '@angular/material/dialog';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatExpansionModule } from '@angular/material/expansion';
import { MatCardModule } from '@angular/material/card';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    MatCardModule, MatFormFieldModule, MatInputModule, MatDialogModule, BrowserAnimationsModule, MatExpansionModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

browserlist (note that IE 9-11 is enabled)

# This file is used by the build system to adjust CSS and JS output to support the specified browsers below.
# For additional information regarding the format and rule options, please see:
# https://github.com/browserslist/browserslist#queries

# You can see what browsers were selected by your queries by running:
#   npx browserslist

> 0.5%
last 2 versions
Firefox ESR
not dead
IE 9-11 # For IE 9-11 support, remove 'not'.

tsconfig.json (note the es5)

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es5",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

Here is the stackblitz project, but you can't debug with IE11. In my real project I have a ngFor, which creates multiple input elements.

Is there a solution for this?

Demonstrating the issue

IE calls input change event on page load

ng version

Angular CLI: 8.3.22
Node: 10.14.2
OS: win32 x64
Angular: 8.2.14
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.803.22
@angular-devkit/build-angular     0.803.22
@angular-devkit/build-optimizer   0.803.22
@angular-devkit/build-webpack     0.803.22
@angular-devkit/core              8.3.22
@angular-devkit/schematics        8.3.22
@angular/cdk                      7.3.6
@angular/cli                      8.3.22
@angular/material                 7.3.6
@ngtools/webpack                  8.3.22
@schematics/angular               8.3.22
@schematics/update                0.803.22
rxjs                              6.5.4
typescript                        3.5.3
webpack                           4.39.2

step-by-step instructions

  • ng new test (no Angular routing, SCSS)
  • replace app.component.html and app.component.ts (add mat-form-field and onValueChange())
  • add MatFormFieldModule, MatInputModule and BrowserAnimationsModule to app.module.ts
  • ng add @angular/material (Indigo-pink, no HammerJS, browser animations yes)
  • edit browserlist (remove NOT at the line IE 9-11)
  • set target to es5 in tsconfig.json

Solution

  • My solution is based on this:

    app.component.html

    <mat-form-field>
      <input matInput placeholder="test" (input)="onValueChange($event)" (focus)="onFocus($event)" (blur)="onFocus(null)">
    </mat-form-field>
    

    app.component.ts

    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss']
    })
    
    export class AppComponent implements OnInit {
      selectedElement: any;
    
      ngOnInit() {
      }
    
      onValueChange(event) {
        if (event && this.selectedElement && event.target == this.selectedElement) {
            // do whatever you like
            console.log(event.target.value)
        }
      }
    
      onFocus(event) {
        if(event){
           this.selectedElement = event.target;
        } else {
           this.selectedElement = null; 
        }
        //console.log(this.selectedElement);
      }
    }
    

    With this the code is only be called if the user changes a value (enter/deleting some characters). You have to do this for every input/textarea field, which has a placeholder and uses the (input).