您可以做一些事情来使其工作,而不是为所有组件订阅一个公共主题或事件发射器,而是为每个组件动态创建一个独特的主题。所以它不会为所有组件触发。首先,您需要为每个组件提供一个唯一的 componentName,或者您可以使用 id。
data = [
{
id: 1,
name: 'Leanne Graham',
username: 'Bret',
email: 'Sincere@april.biz',
button: 'Button #1',
componentName:"component"+1
}
第 2 步:根据 componentNae 为每行创建注册主题。每次关闭点击相应的订阅将调用,然后你可以从这里删除组件
ngOnInit() {
this.renderValue = this.value.toString().toUpperCase();
this.InjiService.componentSubjects[this.rowData.componentName] = new Subject();
this.InjiService.componentSubjects[this.rowData.componentName].subscribe(()=>{
this.InjiService.removeComponent(this.expanededComp);
this.expanededComp = null;
//this.renderValue = this.value.toString().toUpperCase(); //"Open";
this.isOpen = false;
//firing the change detection manually
this.ref.markForCheck();
});
}
请确保在您的服务中声明了 componentSubjects
export class InjiService {
public componentSubjects: { [name: string]: Subject<any> } = {};
Working sample