为什么子组件看不到变化?

时间:2018-02-22 07:06:43

标签: angular

这里有一个example,有2个数组。我试图通过@Input和@Output进行双向数据绑定。子组件:

public handleEvent(isSelected:any){
  this.user = isSelected;
}
public isSelected(user) {
    console.log(this.selected);
    return this.selected.indexOf(user) !== -1;
}
@Output('childData') outgoingData = new EventEmitter<string>();
public sendData(data:any){
    this.outgoingData.emit(data);
}
@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);
    }
}

HTML:

p([ngClass]="{ 'selected': 'isSelected(user)' }" (event)="clickOnUser()", , (ngModelChange)="sendData($event)") {{user.Name}}

父组件:

sampleChildData;
public handleEvent(childData:any){
    this.sampleChildData = childData;
}

不幸的是,没有任何作用:列表中的所有项目都是红色的,即它们都被选中。应该怎么做?

0 个答案:

没有答案