Answer the question
In order to leave comments, you need to log in
How to implement two-way binding in Angular?
There is a small example with two arrays: https://plnkr.co/edit/GN8YNJqBkTRhnGFKkDfs?p=preview
Is it possible to implement this via @Input like this: everything is the same, but both arrays are passed to the child component?
I tried, rendered an array in the child component, but all elements are red. The click is not processed.
child component:
public handleEvent(isSelected:any){
this.user = isSelected;
}
public isSelected(user) {
console.log(this.selected);
return this.selected.indexOf(user) !== -1;
}
@Output() event: EventEmitter<any>=new EventEmitter();
clickOnUser(user) {
if (this.selected.indexOf(this.user) !== -1) {
this.selected.splice(this.selected.indexOf(this.user), 1);
} else {
this.selected.push(user);
}
}
Разметка:
<code lang="html">
p([ngClass]="{ 'selected': 'isSelected(user)' }" (event)="clickOnUser()") {{user.Name}}
</code>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question