2

I'm new to Angular. I'm using below code to attach file from folder,

   <input #fileInput type="file"  />
<button type="button" (click)="fileInput.click()">trigger</button>

After selecting specific file the UI looks like the attached imageenter image description here

Now i'm trying to remove the selected file name with the default text "No file chosen" next of Choose File button like this image, when clicking some buttonenter image description here

Can anyone suggest me an idea to achieve this? Thanks in advance.

3 Answers 3

9

Just reset the value to empty for that element. Like below -

<input #fileInput type="file"  />
<button type="button" (click)="fileInput.click()">trigger</button>
<button type="button" (click)="reset(fileInput)">Reset</button>

reset(element) {
    element.value = "";
}

Working Example

Sign up to request clarification or add additional context in comments.

1 Comment

alternatively we can do another method to avid the reset button ************** mainfunction($event.taget.value) { //Upload function logics here event.target.value = '' }); } ******************** like this you can reset after the upload functions was done
2

You can use ViewChild for this.

In your .ts declare viewchild like this =>

@ViewChild('fileInput')
  myVar1: any;

Inside reset method you can do like this =>

this.myVar1.nativeElement.value = '';

Comments

1

this is actually nothing with angular. It can be done with or without angular.

with Angular

<input #fileInput type="file"  />
<button type="button" (click)="fileInput.click()">trigger</button>
<button type="button" (click)="fileInput.value=''">Reset</button>

With JQuery

$('#fileId').value=''

with Pure JS

document.GetElementById('elementId').value = '';

2 Comments

Bro dont mix jQuery with Angular, when we have way with angular
sure, I just wanted to point out that it is actually nothing more then DOM manipulation. but thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.