Skip to content
Advertisement

reusing angular 9 component

I have a file upload component, app-file-upload that has a file type and a list of files variables. Everything works fine until I want to have multiple app-file-upload components on the same page.

What I am expecting is that when I click on the first file upload button, it would populate the list on the first app-file-upload. Then when I click on the second file upload button, the uploaded files would go to the second component’s list.

What actually happens is that the second list of files would go to the first component’s list. It is as if the second component is just a reference of the first.

Question: I know that angular services are created as singletons. So are components?? it wouldn’t make sense because components are to be reused. Or am I doing something wrong?

Advertisement

Answer

Thanks to @xDrago. I found the issue. I have a label that is for the input of type file (code below). All I need to do I generate a random number and assign to the for of the label and the id of the input.

 <label for="file-{{uniqueNumber}}" (click)="handleUploadClick()">
        <th-icon-upload></th-icon-upload>
        {{buttonText}}
    </label>
 <input type="file" id="file-{{uniqueNumber}}" [attr.multiple]="multiple ? '' : null" (change)="handleFileInput($event.target.files)" accept="{{acceptableFiles}}" 
        [attr.disabled]="successFilesCount >= maximumNumberOfFiles ? '' : null" />
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement