I have a parent and a child components:
Parent JS
export class ListComponent {
public users = [{Id: 1, Name: 'One'}, {Id: 2, Name: 'Two'}, {Id: 3,
Name: 'Three'}];
public selected = [{Id: 2, Name: 'Two'}];}
Parent HTML
my-child([users]="users", [selected]="selected")
Child JS
export class ChildComponent {
@Input() public users: string[];
@Input() public selected: string[];
public isSelected(user.Name) {
return this.selected.indexOf(user) !== -1;
}
public clickOnUser(user.Name) {
if (this.selected.indexOf(user.name) !== -1) {
this.selected.splice(this.selected.indexOf(user.Name), 1);
} else {
this.selected.push(user.Name);
}
}
}
Child HTML
div(*ngFor="let user of users")
p(
[ngClass]="{ selected: isSelected(user.Name) }",
(click)="clickOnUser(user)") {{user.Name}}
I need the second item in a list to be selected by default (I have a class 'selected' in my SASS file for that). For now there's an error in IDE:
"public isSelected(user.Name) {" - dot is highlighted, because comma expected.
"public clickOnUser(user.Name) {" - the same error.
Here we can see almost identical example and it works fine (the difference in this case is in the arrays - they don't contain objects). So, how to make the second item in my list selected by default?