将 app.ts 中的代码替换为以下代码即可。Zone.js 将在回调执行后检测更改。
//our root app component
import {Component, NgModule,NgZone} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { FileDropDirective } from './file.drop.directive'
@Component({
selector: 'my-app',
template: `
<div>
<div file-drop class="drop-area" readAsItems="yes"
(droppedFiles)="onFilesDrop($event)">Recursive func</div>
<div file-drop class="drop-area"
(droppedFiles)="onFilesDrop($event)">Non-recursive func</div>
<div *ngFor="let f of files">{{ f }}</div>
</div>
`,
styles: [`
.drop-area {
width: 100%;
height: 20vh;
background-color: #f1f1f1;
border: 1px solid red;
margin: 5px 0;
}
`]
})
export class App {
name:string;
files: string[];
constructor(private zone:NgZone) {
this.name = 'Angular2'
this.files = [];
}
onFilesDrop(files) {
this.zone.run(()=>{
files.forEach((f) => {
this.files.push(f.name);
})
console.log(this.files);
});
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App, FileDropDirective ],
bootstrap: [ App ]
})
export class AppModule {}