Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
102
rated 0 times [  106] [ 4]  / answers: 1 / hits: 7182  / 4 Years ago, mon, may 4, 2020, 12:00:00

I'm trying to update the text in the span, using the latest Angular. However, I do not understand clearly how lifecycle hooks and update work in Angular. Issue is with fileName - I bind the data and it gets the initial value when the page loads. However when the data variable updated, I can see changes in the console, but the component itself is not updated.



Shall I use some Lifecycle methods or something else?
I've read: https://angular.io/guide/lifecycle-hooks and didn't make clear for me.



<form (ngSubmit)=putToBucket() class='form-class' >
<label for=image_uploads >Select Image</label>
<input type='file' id=image_uploads (change) ='onFileSelected($event)' class='input-button' multiple>
<span > {{fileName }} </span>
<button class='submit-button' type='submit' >Submit</button>
</form>


@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss']
})


export class DashboardComponent implements OnInit {
constructor(
private http: HttpClient,
private toastr: ToastrService) { }
urlApi = '//myuri api';
respond;
fileName: Array<any> =['Test']


onFileSelected(event) {
//console.log(event.target.files[0].name)
let name = event.target.files[0].name;
this.fileName.push(name)
console.log(this.fileName)



Example of what I see:
enter


More From » angular

 Answers
5

Your fileName is an array so to display it you have to iterate it in .html file or you can change fileName to string and do as shown below.



export class AppComponent  {
fileName=Test;

ngOnInit(){
console.log(this.fileName);
}

onFileSelected(newName){
this.fileName=newName;
console.log(this.fileName);
}
}


.html file



<button (click)=onFileSelected('newFile')>change file name</button>


Working Demo : demo


[#3932] Friday, May 1, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stephonkeandrer

Total Points: 392
Total Questions: 94
Total Answers: 100

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
;