【问题标题】:input event of matInput is called on page load in Internet Explorer 11, when a placeholder is set当设置占位符时,在 Internet Explorer 11 中的页面加载时调用 matInput 的输入事件
【发布时间】:2020-03-10 12:00:58
【问题描述】:

我发现有一个IE bug,设置占位符是调用输入事件为described here。这发生在页面加载时,因此没有用户交互。

我的代码:

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 { }

浏览器列表 (注意IE 9-11已启用)

# 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 (注意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
  }
}

这里是the stackblitz project,但是不能用IE11调试。在我的真实项目中,我有一个ngFor,它创建了多个输入元素。

有解决办法吗?

证明问题

ng 版本

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

分步说明

  • ng new test(无 Angular 路由,SCSS)
  • 替换app.component.htmlapp.component.ts(添加mat-form-fieldonValueChange()
  • MatFormFieldModuleMatInputModuleBrowserAnimationsModule 添加到app.module.ts
  • ng add @angular/material(靛蓝粉色,没有 HammerJS,有浏览器动画)
  • 编辑 browserlist(删除 IE 9-11 行的 NOT)
  • tsconfig.json 中将 target 设置为 es5

【问题讨论】:

    标签: angular angular-material internet-explorer-11 placeholder mat-input


    【解决方案1】:

    我尝试使用 IE 11 浏览器测试您的示例代码。我发现在页面加载时没有调用输入事件。当输入发生变化时,它通常会发生。

    测试结果:

    我建议您再次测试问题并使用新的空项目检查结果。

    如果我错过了测试中的任何步骤,请告诉我。我将再次尝试测试问题以检查结果。

    编辑:------------------------------------------ -------------------------------------------------- -----------------------------

    花了一些时间再次匹配这两个项目后,我发现了不同之处。

    我注意到 app.component.html 中的 HTML 代码中的 placeholder="test" 导致了这个问题。 您在原始帖子中已经提到过。除此之外,我没有发现任何其他差异。它看起来确实像任何错误。

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

    看这里:

    它对我有用,因为我在输入中添加了 Value 属性。我添加它只是为了测试目的,因为显示的是该值而不是占位符文本。我忘记删除了。

    这是我的代码:

    <mat-form-field>
      <input matInput placeholder="test" (input)="onValueChange($event)" value="sample text">
    </mat-form-field>
    

    我不确定为什么 placeholder 会导致此问题。由于 IE 将来只会获得安全更新,我不确定是否会有任何修复。您可以尝试使用解决方法来避免 IE 浏览器中的问题。

    【讨论】:

    • 我编辑了我的问题以向您展示问题。您使用了哪个代码?我的初始代码还是那个应该修复它的代码?我正在使用版本为 11.55.17763.0 的 IE。我的修复似乎目前正在工作,但我必须调整我的真实项目,看看它是否适用于多个元素。
    • 我再次检查存储在我的机器上的测试项目并验证我测试了你的第一个代码,我也再次运行它并验证 onValueChange 没有在页面加载时执行并且它在 IE 11 上运行良好(11.719.18362.0) 浏览器。我想与您确认您使用的是哪个 Angular 版本?
    • Angular 8(见编辑过的问题)。如果您愿意,我可以压缩我的项目并上传。 IE 版本的差异可能是一个提示(以及 Angular 版本)。
    • 您可以在此处查看我的项目属性。 imgur.com/a/xr3S3bu 几乎与小变化相似。您可以尝试共享示例项目。我们将尝试对其进行测试以检查问题。请记住从示例项目中删除任何机密信息。我们不需要任何个人或机密信息。感谢您的理解。
    • Here 你去吧。我已删除 node_modules 文件夹,但您可以使用 npm install 将其恢复。
    【解决方案2】:

    我的解决方案是基于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);
      }
    }
    

    只有在用户更改值(输入/删除某些字符)时才会调用代码。您必须为每个 input/textarea 字段执行此操作,该字段具有占位符并使用 (input)

    【讨论】:

      猜你喜欢
      • 2014-02-19
      • 2011-07-28
      • 2017-07-23
      • 2015-07-15
      • 2013-11-12
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      相关资源
      最近更新 更多