【发布时间】:2017-10-19 00:24:43
【问题描述】:
我有 2 个组件,即主 app.component 和一个 header.component 现在我想在标题中放置一个按钮,该按钮将一个类切换为在主 app.component 中处于活动状态我已经让按钮在 app.component 中工作但我想将按钮移动到标题组件但具有相同的功能
HTML
<div>
<button type="button" id="sidebarCollapse" class="btn btn-primary"
(click)="togglesideBar(); toggleSign();">
<i class="glyphicon glyphicon-{{sign}}"></i>
</button>
</div>
<!--Chat Side Bar-->
<div id="chatsidebar" [ngClass]="{'active': isSideBarActive}">
<app-chatsidebar></app-chatsidebar>
</div>
</div>
APP.COMPONENT.TS
sign = 'chevron-right';
toggleSign() {
if (this.sign === 'chevron-right') {
this.sign = 'chevron-left';
} else {
this.sign = 'chevron-right';
}
}
togglesideBar() {
this.isSideBarActive = !this.isSideBarActive;
}
我尝试将相同的功能放在 header.component.ts 中,并将相同的按钮放在 header.component.html 中,但它似乎不起作用
任何帮助将不胜感激! 谢谢
【问题讨论】:
-
德文在下面的回答是做到这一点的一种方法。取决于您的应用程序的复杂程度,尽管您可能想要查看 ngRx,它是解决此问题的更复杂的解决方案,但也解决了许多其他问题。 youtube.com/watch?v=cyaAhXHhxgk
标签: javascript html angular typescript