Skip to content

Deleting element in ngFor not given correct data after that to display #21068

Closed

Description

@Alkasih

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[X] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

data [
     {name: "jack", background:blob.jack.jpg},
     {name: "rose", background:blob.rose.jpg},
     {name: "philip", background:blob.philip.jpg},
     {name: "paul", background:blob.paul.jpg},
]

I am using a background image for each container with loop of object. And when
deleting one element/object from array in ngFor, it doesn't give the correct background image after that to display, while the object in array and their value are still aranged correctly.

Expected behavior

Minimal reproduction of the problem with instructions

https://stackoverflow.com/questions/47847409/delete-file-value-from-loop-array-will-mess-up-the-container-background-angula

I have opened a question a day ago to SO, but seems like no one can fix it.

What is the motivation / use case for changing the behavior?

Environment


Angular version: 5.0.3


Browser:
- [X] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [X] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Activity

  1. mlc-mlapis commented on Dec 17, 2017

    @mlc-mlapis
    Contributor

    @Alkasih ... link to SO doesn't work ... also a simple demo on Plunker or Stackblitz is expected ...

  2. Alkasih commented on Dec 17, 2017

    @Alkasih
    Author
  3. Ploppy3 commented on Dec 17, 2017

    @Ploppy3

    @Alkasih

  4. mlc-mlapis commented on Dec 17, 2017

    @mlc-mlapis
    Contributor

    @Alkasih ... as @Ploppy3 said ... create a simple repro demo on https://stackblitz.com ... it would be the easiest way for you.

    Remember that the first condition for creating an issue here is to have a working reproduction of the problem if at least possible.

  5. Alkasih commented on Dec 17, 2017

    @Alkasih
    Author

    @Ploppy3 @mlc-mlapis

    This work good with angular 5.0.0

    https://stackblitz.com/edit/angular-stpwi2?file=app%2Fapp.component.ts

    Please this doesn't work with angular 5.0.3

    In angular 5.0.3, if I delete one image after adding it, the background image doesn't set up corretly to its container, means that the background image set to another container.

  6. Alkasih commented on Dec 17, 2017

    @Alkasih
    Author

    Please see the update version, now it doesnt work with this multiple wrapper:

    https://stackblitz.com/edit/angular-stpwi2?file=app%2Fapp.component.ts

    can you inspect what is the problem?

  7. mlc-mlapis commented on Dec 17, 2017

    @mlc-mlapis
    Contributor
  8. Alkasih commented on Dec 17, 2017

    @Alkasih
    Author
  9. mlc-mlapis commented on Dec 17, 2017

    @mlc-mlapis
    Contributor

    @Alkasih ... ah, I am a bit confused ... so you know what is exactly the problem?

  10. Alkasih commented on Dec 17, 2017

    @Alkasih
    Author

    The problem appears when I have multidimensional array to loop it in the view? Can you help, please?

  11. Alkasih commented on Dec 17, 2017

    @Alkasih
    Author

    https://stackblitz.com/edit/angular-stpwi2?file=app%2Fapp.component.ts

    My original code doesn't look like that, I just make it similar. If you have something to ask, you can ask. Not just giving thumbs down and thinking how to fix it actually.

    I am not focusing on this actually. But I want to see if it can be fixed.

  12. Ploppy3 commented on Dec 17, 2017

    @Ploppy3

    @Alkasih Giving thumbs down to the people trying to help you is not a good idea either.

    Can you please explain the bug? All your stackblitz projects seem to work properly.

  13. michaelsaprykin commented on Dec 17, 2017

    @michaelsaprykin

    Stackblitz link worked as expected for me too...Didn’t clearly understand what’s the issue :-)

  14. mlc-mlapis commented on Dec 17, 2017

    @mlc-mlapis
    Contributor

    @Alkasih ... the problem is that you don't use a correct syntax for trackBy function.

    It should be:
    <div *ngFor="...; trackBy: trackByFn"> where

    trackByFn(index, item) {
        return item.id;
    }
    

    where item.id should be globally unique id. Actually your item is empty so it is not possible to use and your code for *ngFor depends only on a relative order of index ... but this index is misleading because you store a selected image just in <input> DOM node directly but adding and removing you are doing on objData.

    So you remove one element from objData[0].image but the relative index of <input> nodes is still the same ... from 0 to the number of nodes ... so it is not possible to exactly identify the right <input> node and the deletion looks that only the last node / image is deleted.

  15. Alkasih commented on Dec 18, 2017

    @Alkasih
    Author

    Dear @mlc-mlapis,

    First of all, thank you for the analyze and that good respond. But, I dont think it is really about trackBy , and even in my view the trackBy doesnt effect anything in that funtionality. When I use a simple array it works good, but when I have nested loop like that, the problem appears. You can try it, and see if it works good with that, tell me how do you make it works good.

    I am sorry if I want to know more about this. I am still learning this framework actually.

  16. added
    area: core
    on Dec 19, 2017
  17. alxhub commented on Dec 19, 2017

    @alxhub
    Member

    Hello, we reviewed this issue and determined that it doesn't fall into the bug report or feature request category. This issue tracker is not suitable for support requests, please repost your issue on StackOverflow using tag angular.

    If you are wondering why we don't resolve support issues via the issue tracker, please check out this explanation.

  18. Alkasih commented on Dec 19, 2017

    @Alkasih
    Author

    @mlc-mlapis

    I am still not sure what the aim of trackBy in angular is. But in this case, it seems that angular make a simple thing not really easy, especially in built-in functionality. However I am glad to know this, it makes it not all people can really have fun with angular. The more complicated it is, the less people can prove that they are a good programmer. I hope angular don't change too much in next version. Thank you for all people here. You all rock!

  19. mlc-mlapis commented on Dec 19, 2017

    @mlc-mlapis
    Contributor

    @Alkasih ... you'r welcome ... I'd like to help if it has a sense ...

    The trackBy is still the simple thing ... when *ngFor inserts nodes to DOM then trackBy provides static and unique key for each node. You can see it as 'primary key' from RDBMS world ... it is the equivalent. And this key is the identifier that allow the next run of *ngFor to find out if a node is in DOM or not ... just using the same key.

    If there is not that key ... for example when you don't use trackBy ... an implicit default key is used instead ... and as the first (default) option is tracking array items using their object identity. It means that if elements have the exact same values as the previous list, Angular will not be able to find out ... is a new list item already present or not? So a simple solution would be ... guarantee that each object identity is unique. Great ... but how to guarantee this condition on real data without exact algorithm?

    As the next it is possible to use relative index in a processed array. But because it has the relative character ... it means that this key always starts at 0 and ends with the number (array.length - 1) and ... it is always true even when you delete some node / element from inside / beginning / end ...

    Angular *ngFor ..... and this is not specific for Angular at all - by that way would work any other algorithm in any language ... isn't able to differentiate ... any relative number is still the same before and after ... 5 = 5. There is no hidden information that could be helpful for anybody how to research what is what.

    So ... the information supplied by trackBy function should be the static and unique primary key.

  20. angular-automatic-lock-bot commented on Sep 13, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  21. locked and limited conversation to collaborators on Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: core

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions