我有一個拉菜單項列表的服務,並在* ng中加載項目。菜單項目列表還包括菜單項目的狀態。Angular 2:函數中的[ngClass]更新條件
服務
buttonlist = [
{id: 1, menu: 'me', status: 'active',children: 2,3,4 },
{id: 2, menu: 'home', status: 'nonactive'},
{id: 3, menu: 'contact', status: 'nonactive'},
{id: 4, menu: 'location', status: 'nonactive'}
];
組件
<ul>
<li *ngFor="let button of buttons " [ngClass]="'active' == buttons[subButton].status ? 'active' : 'hide'"
(click)="execCommand(button)"
>
{{button.id}}
<ul *ngFor="let subButton of button.children " >
<li
(click)="execCommand(button)"
[ngClass]="'active' == buttons[subButton].status ? 'active' : 'hide'"
>
{{buttons[subButton].status}} </li>
</ul>
</li>
</ul>
組件功能
execCommand(button){
button.status = 'non-active';
}
如果我登錄按鈕狀態。它只表示我已經更新了對象數據,但它沒有被渲染。如何使這個按鈕更新狀態顯示活動類?
我的菜單基本上是ID 1是頂級,其餘的都是兒童,onclick我想刪除所有兄弟姐妹的活動,只有活躍的按鈕,我點擊,除非我點擊頂級比只是有頂級活躍和所有的孩子。
我沒有使用路由,因爲我有一個頁面應用程序與幾個級別的菜單項,如果用戶單擊它加載選項卡。
我創建了一個正常工作的潛水員,請您澄清一下您想達到的目標? https://plnkr.co/edit/wvDSzf2mHu39NLkVhc2I?p =預覽 – karser
當用戶點擊兄弟姐妹(頂級或子級別)時,它會將其他兄弟變爲無效,並且它所點擊的項目處於活動狀態。 –
我更新了那個看起來非常類似於你所描述的https://plnkr.co/edit/wvDSzf2mHu39NLkVhc2I?p=preview – karser