0
votes

First component which renders the file select html enter image description here

When I click on browse button I redirect to another component Fileselect component which is second component this.goto('fileselect');

enter image description here

Problem is when I select a file from file component I have to display it in the input field of the first component

When I click on the Select class button I am calling redirect() function.

In file select component I have used service to pass the file name to the first component as shown in the below code

 redirect(){
  this._messageService.filter(this.selectedFile);
  this.router.navigateByUrl('');

}

And in the First Component where I want this file name, I have subscribed to function as shown

ngOnInit() {
   this._messageService.listen().subscribe((m:any) => {
            this.fileName = m
            this.loadXMLasJSON()
            console.log(this.fileName)

});}

I am assigning the variable that is this.fileName in the subscribe function when I console.log I am getting the filename. But the view is not getting updated.

My view code

 <input type="text"  class="form-control image-preview-filename" value="{{fileName}}" />

What could be the problem?

Please help me with this.

2
What is the value of this.fileName? - Radonirina Maminiaina
It is the file name that i have selected like example.xml - Ninad
I would suggest prefer ngModel and second to update the view, inject Changedetectorref in your constructor and in your subscribe method use markForCheck() or detectChanges() methods from Changedetectorref. Call one of these methods after assigning the value of filename - Sumeet Kale
@SumeetKale when i do that i get error ViewDestroyedError: Attempt to use a destroyed view: detectChanges - Ninad
Got it. Your navigation method is wrong I guess. Are you using normal router outlet or named router outlet. I think you can solve this using named router outlet. In which you can navigate the fileselect route in a named router outlet which should prevent your first component to not get destroyed on navigation - Sumeet Kale

2 Answers

0
votes

Use [ngModel] instead of value:

<input type="text"  class="form-control image-preview-filename" [ngModel]="fileName" />
-2
votes

Kindly remove double quotes from value attribute.

Change it to:

<input type="file" class="form-control image-preview-filename" value={{fileName}} />