-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve documentation of behavior for ngFor's trackBy #40461
Comments
It is interesting to note that only items that are moved "earlier" in the list are re-rendered. When "one" and "two" are moved later in the array, by items being added/moved ahead of them, they do not re-render. |
Hi, I am facing the same problem. If we choose to use the ID of an object for tracking changes, the array stays in the same size and only the order of the items changes but not the IDs of the objects, we do not want the items to re-render. This behavior is an exactly what I expect from the definition of Best regards |
I have played with the demo and everything is working as intended as far as I can tell which leads me to believe that I don't understand the issue. First the reproduction needs instructions as to what I should click on and what the expectations vs actual behavior is. Because, when I click on changes it looks fine to me, so unless I understand where your expectations differ from what is happening it is hard for me to understand the issue. I have few hunches as to what the issue may be. First let's talk about rendering. Perhaps documentation needs to be updated, but The purpose of
There is no way to know which behavior the developer intended, hence we provide A way to think about When looking at your reproduction the array is changed so as to re-order the rows. So is this an issue with better documenting of what |
Ok, this makes things a little bit more clear, but still I don't understand how trackBy affect the animation. I would like to provide an example: @Component({
selector: "app-items",
templateUrl: "./items.component.html",
styleUrls: ["./items.component.scss"],
})
export class ItemsComponent implements OnInit {
items$: Observable<Array<Item>>;
constructor(
private readonly comService: CommunicationService
) { }
ngOnInit() {
this.items$ = this.comService.subscribe();
}
trackById(index, item: Item) {
return item.id;
} <ng-container *ngFor="let item of items$ | async; trackBy: trackById">`
<app-item [item]="item" class="fade-id-animation">
</app-item>
</ng-container>` The problem is that the fade in animation is triggered whenever the order of the array changes and not when a new item with a new ID is inserted to the list. Reading the documentation of |
Angular needs to associated the DOM nodes with an item in the This may be useful: https://ultimatecourses.com/blog/angular-animations-how-to-animate-lists See: https://filipows.github.io/angular-animations/ and https://stackblitz.com/edit/angular-animations-lib-demo |
Still doesn't make sense to me. I created a simple test where I use a static size array with a few items (each one has an unique ID) and trackById() function. Doesn't |
Perhaps this example helps to clarify: https://stackblitz.com/edit/angular-ivy-d6avrf?file=src%2Fapp%2Fapp.component.html In this you can see that the animations are indicating that items have moved in the track by Compare this to the track by index scenario, ngFor is unable to tell that the objects have reordered. Instead it assumes that they have not changed order (since the |
Perhaps you are trying to do something which requires Angular animations (not just browser?) https://stackblitz.com/edit/angular-ivy-dwln8h?file=src/app/app.component.html |
@petebacondarwin I still don't get it. Why in your example |
The reason why the "trackBy const obj1 = {id: 1};
const obj2 = {id: 1};
obj1 !== obj2
obj1.id === obj2.id without the |
I wonder if we can provide better/clearer documentation for |
Clarify the prupose of the tracking function and document how to create a good one. Fixes angular#40461
Clarify the prupose of the tracking function and document how to create a good one. Fixes angular#40461
Clarify the prupose of the tracking function and document how to create a good one. Fixes angular#40461
I've cleaned up the docs in #42329, hopefully these improvements clarifies the purpose of track by and clear up the confusion. |
Clarify the prupose of the tracking function and document how to create a good one. Fixes angular#40461 PR Close angular#42329
Clarify the prupose of the tracking function and document how to create a good one. Fixes angular#40461 PR Close angular#42329
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
馃悶 bug report
Affected Package
The issue is caused by package @angular/core
Description
Array of objects (
interface User { id : number, name: string }
) is rendered by*ngFor
withtrackBy
The items in the array are added and deleted by the client implementation
Instead of relying on
id
of each element, the array items their indices changed are reordered, are being rerenderedEven though I am passing item.id in the trackBy function, Angular is re-rendering the array items when the order of the items in the array changes
So I suppose
trackBy
will identify only changed items and rerender them only but every moved item is rerendered as well - unexpected behaviorDemo
馃敩 Minimal Reproduction
馃實 Your Environment
Angular Version:
Angular version - v10.1.5
The text was updated successfully, but these errors were encountered: