Angular 8 code clarification

25 views
Skip to first unread message

learningA

unread,
Sep 29, 2019, 8:02:39 PM9/29/19
to Angular and AngularJS discussion
I'm working through a youtube tutorial and came across a code im not familiar with and need some clarification.

    isSelected(memberList){
   
return memberList.id === this.selectedId;    
   
}

is the code:    return memberList === this.selectedId  like a foreach loop that iterating through the memberList array to match a selectedId?

memberList is a list of test users.

isSelected function which is bind to a class

 <li  *ngFor="let user of memberList"  [class.selected]="isSelected(memberList)">
       
<span class="badge" >{{user.id}}</span>
        {{user.name}}
     
</li>

the author of the tutorial demonstrating how to code the back button and how the list should be highlighted. 

Sander Elias

unread,
Oct 1, 2019, 3:29:39 AM10/1/19
to Angular and AngularJS discussion
Hi LearningA,

I think that code faulty. You should ask the author about this. But without any context, I would say this is a bug.

Regards
Sander

Daniel Brookes

unread,
Oct 1, 2019, 6:16:08 AM10/1/19
to ang...@googlegroups.com
Looks to me like the isSelected function is expecting an individual "member" rather than memberList.

memberList.id === this.selectedId will simply return true or false depending on if the property "id" of the object "memberList" is equal to this.selectedId. There is no looping involved. I would imagine it should be more like:

isSelected(member) {
  return member.id === this.selectedId;
}

assuming that the function is supposed to return a true/false value. There are other methods to find the selected item in the array if you want to return the member itself - for example something like:

selectedMember(memberList) {
  return memberList.find(i => i.id === this.selectedId);
}

Hope this helps.

--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/8cf814e7-3020-42f8-a218-8ca432c93020%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages