【发布时间】:2020-09-27 21:23:26
【问题描述】:
原始行为:单击按钮时,它会切换 div(关闭以打开,打开以关闭) ChildComponent.html
<div *ngIf="showFiller" class="sidenav">
<p>Hey</p>
</div>
<div class="content">
<button (click)="showFiller = !showFiller" mat-raised-button>
Toggle extra text
</button>
<p> Hello </p>
</div>
子组件.ts
showFiller = false;
请求:现在我需要将按钮移动到父组件,但在子组件中需要相同的行为。所以按钮应该在父级中,动作应该在子级中表示 这是我试过的代码
父组件.html
<mat-icon mat-button (click)="toggle()" class="index">menu</mat-icon>
父组件.ts
show: boolean = true;
toggle(){
this.show = !this.show;
this.show;
console.log(this.show);
}
子组件.html
<div *ngIf="showFiller" class="sidenav">
<p>Hey</p>
</div>
<div class="content">
<!-- <button (click)="showFiller = !showFiller" mat-raised-button>
Toggle extra text
</button> -->
<p> Hello </p>
</div>
子组件.ts
@Input() show: boolean;
showFiller: boolean = true;
ngOnInit(): void {
this.showFiller = this.show;
console.log(this.showFiller);
}
ngOnChanges(changes: boolean) {
this.showFiller = this.show;
console.log(this.showFiller);
}
【问题讨论】:
标签: angular typescript