3

I am working on angular 2 project and I am having an issue when I am trying to change the list . NgFor not recognizing the changes , and displaying only the list loaded at first time .

here is an example code when I am loading all list and imminently after loading I reset it with null . the view still displaying all the list ...

this is my component constructor for example :

 constructor( private songService : SongService)
    this.songService.getSongs()
         .subscribe(songsList => {
             this.songs = songsList;
         });

    this.songs = null;

}

and this is the html :

<div class="row">
    <div  *ngFor= "let song of songs" class="col-md-4">
     <app-song-item [song]="song"></app-song-item>
    <br>
    </div>
</div>
8
  • Please post your whole code, not an example. If you want to post an example, then make a MCVE. Commented Mar 20, 2018 at 9:14
  • You are trying to display data which is not there yet Commented Mar 20, 2018 at 9:15
  • @trichetriche this is all code Commented Mar 20, 2018 at 9:20
  • Do you want to display "songsList" in the UI? or You dont want to ? You are saying, "I am loading all list and imminently after loading I reset it with null . the view still displaying all the list ". Commented Mar 20, 2018 at 9:24
  • Set a breakpoint at this.songs = songsList (inside the subscribe) and one at this.songs = null to see in which order the songlist is changed. The subscribe callback is probably called after resetting the list to null. Commented Mar 20, 2018 at 9:30

4 Answers 4

6

Loops in Angular sometimes screw up, in the way that they don't track your items the way you would want it to.

To prevent that, you can use a custom track by function like this

<div *ngFor="let song of songs; let i = index; trackBy: customTB" class="col-md-4">

In your TS

customTB(index, song) { return `${index}-${song.id}`; }

This way, you set up a custom trackBy function, that will update your view (the view wasn't getting updated because the tracking ID wasn't changing).

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

Comments

2

The reason why you are still seeing your list is because it is async. You can't be sure when the subscribe method is executed. It can be be direct, within seconds, take hours or not even at all. So in your case you are resetting the list before you are even getting one.

constructor( private songService : SongService)
  this.songService.getSongs()
    .subscribe(songsList => { //Might take a while before executed.
      this.songs = songsList;
    });

  this.songs = null; //executed directly
}

The above explanation might be the cause of your problem, but there could also be another explanation. The constructor is only called when the component is created. Changing a router parameter doesn't necessarily create a component. Angular might re-use the component if it can.

2 Comments

thank you I understand what are you saying , I also try do it with variable type of number. on the constructor number = 5, and on other function I change it to 6 , the console shows that number is now 6 but the view not changing . and of course in the html I bind the number {{number}}
Maybe you can create a working example demonstrating your problem.
1

Instead of null you should set an empty array, also have it inside a method, otherwise it never gets called

 this.songService.getSongs()
         .subscribe(songsList => {
             this.songs = songsList;
         });
clear(){
    this.songs = [];
}

2 Comments

thank you , but its not solving the problem , still shows all list
you have to call the method from the template
-1

Try this

constructor(private songService: SongService) {
    this.songService.getSongs()
        .subscribe(songsList => {
            this.songs = songsList;
            this.reset();
        });
}

reset() {
    this.songs = [];
}

1 Comment

this is wokring , but in any other place I am trying to do it its not working. as I commented in other posts I also tried with number initialized with 5 and bind it in the html code, in other function I increase the number but the view does not recognize the change

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.