我发现有一个IE错误,其中设置占位符将输入事件调用为在此描述。这发生在页面加载上,因此没有用户交互。
我的代码:
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
}
}这里是stackblitz项目,但您不能使用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 (无角路由)mat-form-field和onValueChange())MatFormFieldModule、MatInputModule和BrowserAnimationsModule添加到app.module.ts中ng add @angular/material (靛蓝-粉红色,没有HammerJS,浏览器动画是的)target中将es5设置为tsconfig.json发布于 2020-04-08 11:21:45
我的解决方案是基于这的
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)。
发布于 2020-03-11 03:30:15
我试着用IE11浏览器测试您的示例代码。我发现输入事件不是在页面加载时被调用的。当输入发生更改时,通常会发生这种情况。
测试结果:

我建议您再次测试该问题,并使用一个新的空项目检查结果。
如果我错过了测试的任何步骤,请告诉我。我将再次尝试测试这个问题,以检查结果。
:------------------------------------------------------------------------------------------------------------------------- 编辑
在花了一些时间再次匹配这两个项目之后,我发现了不同之处。
我注意到placeholder="test"在app.component.html中的HTML代码中引起了这个问题。,你已经在原来的帖子中提到过了,除了这个,我没有任何其他的区别。它看起来像任何一只虫子。
<mat-form-field>
<input matInput placeholder="test" (input)="onValueChange($event)">
</mat-form-field>见这里:

因为我在输入中添加了值属性,所以它在我这边工作。我添加它只是为了测试目的,因为这个值被显示,而不是占位符文本。我忘了移除它。
这是我的密码:
<mat-form-field>
<input matInput placeholder="test" (input)="onValueChange($event)" value="sample text">
</mat-form-field>我不知道为什么占位符会导致这个问题。由于IE只会在未来得到安全更新,我不确定是否会有任何修复。您可以尝试使用解决方案来避免IE浏览器中的问题。
https://stackoverflow.com/questions/60617214
复制相似问题