简体   繁体   English

(change) vs [(ngModel)] 与 Angular 中的输入类型文件

[英](change) vs [(ngModel)] with input type file in Angular

I am new to Angular and got confused between (change) event and (ngModel).我是 Angular 的新手,并且对 (change) 事件和 (ngModel) 感到困惑。 Are both of them used for tracking state of the form?它们都用于跟踪表单的状态吗? Can I use ngModel as (change) or which one is better?我可以使用 ngModel 作为(更改)还是哪个更好? The problem is that when I have a form with input type="file".问题是当我有一个输入类型=“文件”的表单时。

When I use (ngModel) <input type="file" [(ngModel)]="link" name="link"/> and get the value by this.link .当我使用 (ngModel) <input type="file" [(ngModel)]="link" name="link"/>并通过this.link获取值this.link In the backend, it returns a null object {}.在后端,它返回一个空对象 {}。

But when I use (change) <input type="file" name="link" (change)="fileUploadChange($event)"/> and get the file with function但是当我使用 (change) <input type="file" name="link" (change)="fileUploadChange($event)"/>并使用函数获取文件时

fileUploadChange(event){ this.link = event.target.files[0]; }

In the backend, it returns the file path as I want.在后端,它根据需要返回文件路径。 {link: {path: "C://..." }} {链接:{路径:“C://...”}}

So can I use (ngModel) for input type="file" in any way?那么我可以以任何方式将 (ngModel) 用于 input type="file" 吗?

  • [(ngModel)] vs (change): [(ngModel)] vs (change):

  • [(ngModel)]: [(ngModel)]:

    we need to import formsModule in the module in which we want use ngModel.我们需要在要使用 ngModel 的模块中导入 formsModule。 ngModel is used for to way binding. ngModel 用于方式绑定。 it's continues track your each input.它会继续跟踪您的每个输入。 ngModel is not formControlName and not formControl as per angular. ngModel 不是 formControlName 也不是按角度的 formControl。

  • (change): (改变):

    On change event is called only when value is changed from it's previous state.仅当值从其先前状态更改时才调用 On change 事件。 in on change event we need to pass the event and need to fetch value from event like.在 on change 事件中,我们需要传递事件并需要从事件中获取值。 event.target.value . event.target.value .目标.值。

    [(ngModel)] and (change) event with input type = "file" . [(ngModel)] 和 (change) 事件,输入类型 = "file" 。

    if we are using ngModel with file input we only get the fakepath, like.如果我们在文件输入中使用 ngModel,我们只会得到假路径,比如。 C:\\fakepath\\Screenshot from 2019-12-13 15-03-17.png C:\\fakepath\\Screenshot from 2019-12-13 15-03-17.png

    and if we are using it with change event:如果我们将它与 change 事件一起使用:

    html html

    <input type="file" (change)="uploadFile($event)">

    ts ts

    uploadFile(event){ this.file1 = event.target.value; this.file2 = event.target.files[0]; console.log(this.file1) // in this case we only get fakepath same as we get in ngModel. console.table(this.file2) // in this case we get object with data like, name, lastModified, lastModifiedDate, size and type. }

    and i personally think change event is better to use with input type file.我个人认为更改事件更适合与输入类型文件一起使用。

To answer your question about which is better, over the years the trend in Angular has been to move away from ngModel, and two-way binding (which is currently done using ngModel).为了回答您关于哪个更好的问题,多年来 Angular 的趋势是远离 ngModel 和双向绑定(目前使用 ngModel 完成)。 I remember hearing that two-way binding was considered by the Angular team as somewhat of a mistake that should never have existed in AngularJS.我记得听说 Angular 团队认为双向绑定是一个错误,AngularJS 中不应该存在。 It was the default behavior for properties back in AngularJS, and once Angular 2 came out it was only possible through ngModel.这是 AngularJS 中属性的默认行为,一旦 Angular 2 出现,它只能通过 ngModel 实现。 In summary, your approach of using (change) is better than ngModel and more inline with the current recommended approach.总而言之,您使用 (change) 的方法比 ngModel 更好,并且更符合当前推荐的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM