A component gets (via @Input()) 2 arrays: "users"
[{Id: 1, Name: 'One'}, {Id: 2, Name: 'Two'}, {Id: 3, Name: 'Three'}]
and "selectedUsers":
[{Id: 2, Name: 'Two'}]
and outputs items on a page: One Two Three
"Two" - is highlighted as it is contained in array "selectedUsers". How to add item from "users" array to "selectedUsers" and vice versa by click (and highlight clicked item)?
Parent component:
users = [
{Id: 1, Name: 'One'},
{Id: 2, Name: 'Two'},
{Id: 3, Name: 'Three'}
];
usersSelected = [
{Id: 2, Name: 'Two'}
];
HTML:
<app-item (*ngFor="let u of users", [user]="u")></app-item>
Child component:
@Input() user;
isActive: boolean = false;
toggleClass(){
this.isActive = !this.isActive;
}
HTML
<p [class.active]="isActive" (click)="toggleClass()">{{user.Name}}</p>
Here's another try but it should be done with @Input/@Output: https://plnkr.co/edit/cjaij4aQuvN4Tk4ZE0ez?p=preview
selectedof typeboolean(true/false). That is cleaner/easier to use then copying/removing users between the 2 arrays. Also I do not believe that is valid template syntax in your html.*ngFor="let u of users", [user]="u"is not a reference so it's an invalid syntax.