AngularMaterial动态菜单内容不显示
创始人
2024-10-28 09:01:35
0

检查动态数据是否正确传递给菜单组件,并确保使用ChangeDetectorRef触发变化检测。

示例代码:

在组件中声明变量并初始化:

menuItems: MenuItem[] = [ {name: 'Dashboard', link: '/dashboard'}, {name: 'Profile', link: '/profile'}, // add more items dynamically ];

在模板文件中绑定变量:

在组件中更新菜单项:

import { Component, ChangeDetectorRef } from '@angular/core'; import { MenuItem } from './menu-item.model';

@Component({ selector: 'app-menu', templateUrl: './menu.component.html', styleUrls: ['./menu.component.css'] }) export class MenuComponent { menuItems: MenuItem[] = [ {name: 'Dashboard', link: '/dashboard'}, {name: 'Profile', link: '/profile'} ];

constructor(private cdr: ChangeDetectorRef) {}

addMenuItem(name: string, link: string) { this.menuItems.push({name, link}); this.cdr.detectChanges(); } }

在另一个组件中调用addMenuItem方法:

import { Component } from '@angular/core'; import { MenuComponent } from './menu/menu.component';

@Component({ selector: 'app-root', template: , }) export class AppComponent { constructor(private menu: MenuComponent) {}

addMenuItem() { this.menu.addMenuItem('New Item', '/new-item'); } }

相关内容

热门资讯

围绕透视问题!德州机器人代打脚... 围绕透视问题!德州机器人代打脚本(透视)对战互娱怎么开辅助(原来存在有辅助辅助器)-哔哩哔哩1、游戏...
透视挂透视!epoker有透视... 您好,新众乐辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这...
近日!红龙poker有辅助吗(... 近日!红龙poker有辅助吗(透视)新道游辅助软件下载(总是真的有辅助安装)-哔哩哔哩新道游辅助软件...
经调查!破解辅助插件wepok... 经调查!破解辅助插件wepoker(透视)悟空大厅辅助器(果然真的是有辅助辅助器)-哔哩哔哩亲,关键...
据统计!hh poker辅助有... 据统计!hh poker辅助有用吗(透视)创思维辅助器(其实有辅助神器)-哔哩哔哩1、让任何用户在无...
此事备受玩家关注!hhpoke... 此事备受玩家关注!hhpoker有后台操控吗(透视)雀神挂件价格(竟然是有辅助插件)-哔哩哔哩1、雀...
连日来!wepoker辅助器激... 连日来!wepoker辅助器激活码(透视)闲来辅助工具(确实是有辅助工具)-哔哩哔哩1、完成闲来辅助...
透视代打!wepoker底牌透... 透视代打!wepoker底牌透视脚本(透视)潮友会鱼虾蟹怎么发现软件控制(原来存在有辅助下载)-哔哩...
透视模拟器!聚星ai辅助工具激... 透视模拟器!聚星ai辅助工具激活码(透视)微信大a辅助(好像真的是有辅助app)-哔哩哔哩透视模拟器...
透视辅助!wepoker破解工... 透视辅助!wepoker破解工具(透视)潮汕馆透视牌(总是真的是有辅助平台)-哔哩哔哩1、潮汕馆透视...