Using @Output hide to some child component

拥有回忆 提交于 2019-12-25 09:35:20

问题


My issue is I would replace/hide some child components (Child1, 2 and 3) by a "subchild component" each time the toggleSubChild() function is called using @Output decorator (it's just an idea, maybe there's better way to do that); So each time the subChild is called then it delete the Child1, 2 and 3 from the parent component using the EventEmitter value open/close. So the hierarchy is Parent ==> Child1 2 and 3 ==> SubChild I hope I'm clear enough..if not feel free to ask..thanks in advance for the help..

parent.html:

<div><button (click)="toggleChild"></button>
  <div [hidden]="hideMePartially">
   <child1 [toggleMe]="toggleVar"></child1>
   <child2 [toggleMe]="toggleVar"></child2>
   <child3 [toggleMe]="toggleVar"></child3>
  </div>
</div>

parent.ts:

toggleChild() {
    this.toggleVar = !this.toggleVar;
}

child1.html:

<div ngIf*="toggleMe">
 <button (click)="toggleSubChild"></button>
 <subChild [hideMePartially]="hideItUp"></subChild>
<div>

child1.ts:

@Input() toggleMe: boolean;
@Output() open: EventEmitter<any> = new EventEmitter();
@Output() close: EventEmitter<any> = new EventEmitter();

 toggleSubChild() {
     this.hideMePartially = !this.hideMePartially;
     if (this.hideMePartially) {
       this.open.emit(null);
    } else {
      this.close.emit(null);
    }
    console.log(open);
    console.log(close);
   }

subChild.html:

<span *ngIf="hideMePartially">Some content</span>

subChild.ts:

@Input() hideMePartially: boolean;

来源:https://stackoverflow.com/questions/45285481/using-output-hide-to-some-child-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!