Access fileupload inner component of ng-template in Angular

UI Components for Angular
Post Reply
1stefan
Posts: 2
Joined: 08 Feb 2018, 15:54

08 Feb 2018, 15:57

I'm using fileUpload component from primeng ( https://www.primefaces.org/primeng/#/fileupload ) in a modal component ( https://ng-bootstrap.github.io/#/compon ... l/examples )

When Im selecting the files and display them via `ng-template`, i want to have a button that removes some of the files.

In order to do that, i need to call the `remove` method from the FileUpload component.

My problem is that this doesnt work.

I initially tried to use ViewChild but I get `undefined`. I can't user service, because this means i need to update the package.


Code

Code: Select all

	<ng-template #upload let-c="close" let-d="dismiss">
		<p-fileUpload #fileUpload
						customUpload="true"
						(uploadHandler)="uploadMultiHandler($event)"
						(onSelect)="onFileSelect($event)"
						multiple="multiple"
						accept="image/*"
						maxFileSize="1000000"> 
			<ng-template let-file pTemplate="file">
			  <div class="ui-fileupload-row">
				<div><img [src]="file.objectURL" width="50"/></div>
				<div>{{file.name}}</div>
				<div>{{formatSize(file.size)}}</div>
				<div><button type="button" icon="fa-close" pButton (click)="remove(event, file)"></button></div>
			  </div>
			</ng-template>
			<ng-template pTemplate="content">
			  <ul *ngIf="uploadedFiles.length">
				<li *ngFor="let file of uploadedFiles">{{file.name}} - {{file.size}} bytes</li>
			  </ul>
			</ng-template>    
		  </p-fileUpload>
	</ng-template>

	@ViewChild('fileUpload') fileUpload: FileUpload;

	remove(event: Event, file: any) {
		const index: number = this.uploadedFiles.indexOf(file);
		this.fileUpload.remove(event, index);
	}
Does anyone has any idea?
What am I doing wrong?


Links I've checked:

1. https://stackoverflow.com/questions/437 ... g-template
2. https://angular.io/guide/component-interaction
3. http://plnkr.co/edit/p40g2xGgamfCUk8pShHq?p=preview
4. https://plnkr.co/edit/xrv8Ye5c7p3h9kCgSWjD?p=preview
5. https://github.com/angular/angular/issues/14842

Primaryw
Posts: 26
Joined: 09 Nov 2017, 17:51

01 May 2018, 20:44

Wow, no responses to this one? I have a similar issue.

mszarata
Posts: 1
Joined: 09 May 2018, 14:49

09 May 2018, 14:53

I have managed to do this by simply:

Code: Select all

  @ViewChild('fileUpload') fileUpload: ElementRef;

  remove(event, file) {
    this.fileUpload.remove(event, file);
  }

quekqh
Posts: 7
Joined: 19 Jul 2018, 12:15

19 Jul 2018, 12:35

.remove cannot be found?

Post Reply

Return to “PrimeNG”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 8 guests