我是 Angular 2 的新手。试图在从浏览器中选择的模板表中列出文件名。下面是我的代码
模板.html
<input type="file" id="uploadFile" style="display: none" (change)='onClickUploadDocument($event)' multiple>
<label for="uploadFile" class="btn btn-primary">Upload Documents</label>
<table cellpadding="4" class="grid" >
<thead><tr><th></th><th>Document Name</th><th>Document ID</th><th>Document Type</th><th>Source</th><th>Notes</th><th>Action</th></tr></thead>
<tbody>
<tr *ngFor="let file of files">
<td><input type="checkbox" checked="checked"></td>
<td >{{file.name}}</td>
<td>DC2352</td>
<td><input type="text" class="form-control"></td>
<td><input type="text" class="form-control"></td>
<td><input type="text" class="form-control"></td>
<td>
<a href="#"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></a>
<a href="#"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a>
<a href="#"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
</td>
</tr>
</tbody>
</table>
组件.ts
import {Component, OnInit, OnChanges} from '@angular/core';
import {NgClass} from '@angular/common';
import {ROUTER_DIRECTIVES} from '@angular/router-deprecated';
@Component({
selector: 'documentManagement',
templateUrl: 'app/dashboard/features/documents/documentManagement/documentManagementTemplate.html',
directives: [ROUTER_DIRECTIVES, NgClass]
})
export class DocumentManagementComponent implements OnInit, OnChanges {
files: any[];
ngOnInit(): void {}
ngOnChanges(): void {}
onClickUploadDocument(event){
console.log("clicked");
var file = event.target.files;
console.log(file);
for (var i = 0; i < file.length; i++) {
var fileInfo = file[i];
console.log(fileInfo);
this.files = fileInfo;
}
}
}
如果我尝试应用 *ngFor,我会收到以下错误
错误
Error: Cannot find a differ supporting object '[object File]' of type 'Jellyfish.jpg'. NgFor only supports binding to Iterables such as Arrays.
at new BaseException (exceptions.ts:14)
at NgFor.Object.defineProperty.set [as ngForOf] (ng_for.ts:48)
at DebugAppView._View_DocumentManagementComponent0.detectChangesInternal (DocumentManagementComponent.template.js:386)
at DebugAppView.AppView.detectChanges (view.ts:243)
at DebugAppView.detectChanges (view.ts:345)
at DebugAppView.AppView.detectViewChildrenChanges (view.ts:267)
at DebugAppView._View_DocumentControlPanelComponent3.detectChangesInternal (DocumentControlPanelComponent.template.js:467)
at DebugAppView.AppView.detectChanges (view.ts:243)
at DebugAppView.detectChanges (view.ts:345)
at DebugAppView.AppView.detectContentChildrenChanges (view.ts:261)
有人能说出错误和解决方案吗?谢谢