I called the detectChanges
of the ChangeDetectorRef
inside the FileUpload
component, then the file started appearing!
ngAfterViewInit(): void {
if (this.fileUpload) {
this.fileUpload._files.push(this.file);
this.fileUpload.cd.detectChanges(); // <-changed here!
this.cdr.detectChanges();
}
}
full code
import {
AfterViewInit,
ChangeDetectorRef,
Component,
ViewChild,
inject,
} from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';
import { FileUpload, FileUploadModule } from 'primeng/fileupload';
@Component({
selector: 'app-root',
standalone: true,
templateUrl: './app.html',
imports: [FileUploadModule],
})
export class App implements AfterViewInit {
cdr = inject(ChangeDetectorRef);
file = new File([''], 'filename');
@ViewChild('fileUpload') fileUpload!: FileUpload;
ngAfterViewInit(): void {
if (this.fileUpload) {
this.fileUpload._files.push(this.file);
this.fileUpload.cd.detectChanges();
this.cdr.detectChanges();
}
}
}
bootstrapApplication(App);
Stackblitz Demo