角文件输入更改事件根本不发射

发布于 2025-02-05 20:34:08 字数 560 浏览 3 评论 0原文

是否有人使用了具有Angular的文件输入。当声明为(更改)时,我无法将更改事件发射,但是当使用Onchange时,它可以使用,但不能使用Angular方法。有人成功使用过(变更)事件吗?不知道我在这里缺少什么。

这有效...

<input hidden mat-input onclick="this.value = null" onchange=”alert(‘works’)” #fileInput type="file">

这不起作用...

<input hidden mat-input onclick="this.value = null" (change)=”alert(‘works’)”  #fileInput type="file">

Has anyone used a file input with Angular. I'm not able to get the change event to fire when declared as (change), but when using onchange it works but not with the angular method. Has anyone successfully used (change) event? Not sure what I'm missing here.

This works...

<input hidden mat-input onclick="this.value = null" onchange=”alert(‘works’)” #fileInput type="file">

This doesn't work...

<input hidden mat-input onclick="this.value = null" (change)=”alert(‘works’)”  #fileInput type="file">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉态萌生 2025-02-12 20:34:08
<input mat-input (change)="onChange()" #fileInput type="file" />
<input mat-input (change)="onChange()" #fileInput type="file" />
病女 2025-02-12 20:34:08
//app.component.html

<div>
   <label for="file">file</label>
   <input hidden id="file" #file name="file" type="file" (change)="onFileSelected($event)">
</div>


//app.component.ts

onFileSelected(event){
  const file = event.target.files[0];
  //do what you want with the event
}
//app.component.html

<div>
   <label for="file">file</label>
   <input hidden id="file" #file name="file" type="file" (change)="onFileSelected($event)">
</div>


//app.component.ts

onFileSelected(event){
  const file = event.target.files[0];
  //do what you want with the event
}
同尘 2025-02-12 20:34:08

您无法访问模板中的全局方法。要解决此问题,您可以添加链接到窗口对象的属性:

@Component({...})
export class AppComponent {
    window = window;
}

并从模板调用它:
&lt; input(change)=“ window.alert('test')” type =“ file”&gt;

另一种方法是在组件方法中使用全局方法:

@Component({...})
export class AppComponent {
    testMethod(text) {
        alert(text)
    }
}

html:html:&lt; input (更改)=“​​ testMethod('test')” type =“ file”&gt;

note :另外,请使用正确的报价标记:“''''' ,不是:“''”

You don't have access to global methods in templates. To fix this you can add a property linked to the Window object:

@Component({...})
export class AppComponent {
    window = window;
}

and call it from the template:
<input (change)="window.alert('test')" type="file">

Another approach is to use global methods in component methods:

@Component({...})
export class AppComponent {
    testMethod(text) {
        alert(text)
    }
}

Html: <input (change)="testMethod('test')" type="file">

Note: also please use proper quotation marks: "''", not: ”‘‘”

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文