使用 Angular 2 beta,我似乎无法得到<input type="file">
上班。
使用诊断,我可以看到其他的双向绑定type
比如text
.
<form>
{{diagnostic}}
<div class="form-group">
<label for="fileupload">Upload</label>
<input type="file" class="form-control" [(ngModel)]="model.fileupload">
</div>
</form>
在我的 TypeScript 文件中,有以下诊断行:
get diagnostic() { return JSON.stringify(this.model); }
难道是不是JSON的问题?其值为null
.
我无法真正验证其价值input
。尽管“选择文件...”旁边的文本已更新,但由于某种原因我看不到 DOM 中的差异。
我认为它不受支持。如果你看看这个DefaultValueAccessor
指令(参见https://github.com/angular/angular/blob/master/modules/angular2/src/common/forms/directives/default_value_accessor.ts#L23 https://github.com/angular/angular/blob/master/modules/angular2/src/common/forms/directives/default_value_accessor.ts#L23)。您将看到用于更新绑定元素的值是$event.target.value
.
这不适用于类型输入的情况file
因为可以到达文件对象$event.srcElement.files
反而。
想要了解更多细节,你可以看看这个plunker:https://plnkr.co/edit/ozZqbxIorjQW15BrDFrg?p=info https://plnkr.co/edit/ozZqbxIorjQW15BrDFrg?p=info:
@Component({
selector: 'my-app',
template: `
<div>
<input type="file" (change)="onChange($event)"/>
</div>
`,
providers: [ UploadService ]
})
export class AppComponent {
onChange(event) {
var files = event.srcElement.files;
console.log(files);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)