Emi*_*ero 6 mouseclick-event angular
我正在尝试在单击按钮上创建一个事件来触发另一个组件,如果再次单击它会减少组件(显示组件的一部分始终可见)..我知道这可以通过[ngClass] ='来完成隐藏'和同一组件中的所有东西,但我不确定它是否是模块化方面的最佳方式.提前致谢
这是我的HTML:
<div class="daydetail">
<h1>Detail of the day</h1>
<button type="button" label="Click"(click)="display('my-displaychild')"></button>
<div>{{my-displaychild}}</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我的组件:
import { DisplayChildComponent } from './displaychild.component';
export class AppliV2Component {
display(vid:DisplayChildComponent) {
console.log(DisplayChildComponent);
}
}
Run Code Online (Sandbox Code Playgroud)
Ham*_*our 21
我认为你应该保持简单使用,*ngIf你可以将布尔值传递给子组件,只使用@Input装饰器隐藏你想要的部分
1.parent HTML
<div class="daydetail">
<h1>Detail of the day</h1>
<button type="button" label="Click" (click)="toggleChild()"></button>
<div>
<child-component [showMePartially]="showVar"></child-component>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
2.parent组件
export class AppliV2Component {
showVar: boolean = true;
toggleChild(){
this.showVar = !this.showVar;
}
}
Run Code Online (Sandbox Code Playgroud)
3.child组件
export class ChildComponent {
@Input() showMePartially: boolean;
// don't forget to import Input from '@angular/core'
}
Run Code Online (Sandbox Code Playgroud)
4.child HTML
<div>
<h1> this part is always visible</h1>
</div>
<div *ngIf="showMePartially">
<h1> this part will be toggled by the parent component button</h1>
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53579 次 |
| 最近记录: |