I have a problem displaying a nested value from Localstorage in Angular. The problem is on the second *ngFor. The first *ngFor display well. The second ngFor has an error of "TypeError: Cannot read property 'toUpperCase' of undefined"? I don't know why the is toUppercase since i didn't use uppercase here
HTML
<mat-list-item role="listitem" *ngFor="let bookmark of bookmarks">
{{ extractNameFromJson(bookmark[1]).id }}
<ul>
<li *ngFor="let extractNameFromJson(bookmark[1])?.cuesData of bookmark">
{{ }}
</li>
</ul>
</mat-list-item>
TS
getAllBookmarks() {
this.bookmarks = Object.entries(localStorage);
console.log(this.bookmarks);
}
extractNameFromJson(obj) {
obj = JSON.parse(obj);
return obj;
}
You need to get the value of each bookMark as a variable in
*ngFor
first. You cannot have a function call while extracting a variable from a loop usingngFor
. One way can be to have apipe
which does this filtering for you while looping, or another way could be to useng-container
to loop and only showlis
if your condtion passes.EDIT: if you want to change the
bookmarks
array in your ts, maybe you can do this:Now, if you have done this, then your HTML can be simply:
EDIT2:
All the above filtering, (map/reduce) would happen at the place where you are populating
bookmarks
.