angular2 递归导航菜单实现方式
看了网上很多源码,基本都是采用循环三级的方式。如果是无限级的菜单,就无法实现了。
菜单格式:
[
{
"title": "Item-1",
"iconClass": "fa fa fa-flask",
"link": "#1",
"notice": 0,
"subMenus": null
},
{
"title": "Item-2", "iconClass": "fa fa-level-down", "link": null, "notice": 0,
"subMenus": [
{
"title": "Item-2-1",
"iconClass": "fa fa fa-flask",
"link": "#2",
"notice": 0,
"subMenus": null
}, {
"title": "Item-2-2",
"iconClass": "fa fa fa-flask",
"link": "#3",
"notice": 0,
"subMenus": null
}]
},
{
"title": "Item-3", "iconClass": "fa fa-level-down", "link": null, "notice": 4,
"subMenus": [
{
"title": "Item-3-1",
"iconClass": "fa fa fa-flask",
"link": "#4",
"notice": 1,
"subMenus": null
},
{
"title": "Item-3-2",
"iconClass": "fa fa fa-flask",
"link": null,
"notice": 3,
"subMenus": [
{
"title": "Item-3-2-1",
"iconClass": "fa fa fa-flask",
"link": "#6",
"notice": 1,
"subMenus": null
},
{
"title": "Item-3-2-2",
"iconClass": "fa fa fa-flask",
"link": "#7",
"notice": 2,
"subMenus": [
{
"title": "Item-4-2-1",
"iconClass": "fa fa fa-flask",
"link": "#6",
"notice": 1,
"subMenus": null
},
{
"title": "Item-4-2-2",
"iconClass": "fa fa fa-flask",
"link": "#7",
"notice": 2,
"subMenus": [
{
"title": "Item-5-2-1",
"iconClass": "fa fa fa-flask",
"link": "#6",
"notice": 1,
"subMenus": null
},
{
"title": "Item-5-2-2",
"iconClass": "fa fa fa-flask",
"link": "#7",
"notice": 2,
"subMenus": null
}]
}]
}]
}]
}
];
AppComponent.ts代码:
import { Component } from '@angular/core';
import {TreeViewComponent} from './treeview/treeview.component';
import {MenuItem} from './treeview/menuItem';;
@Component({
selector: 'my-app',
template: '<ul tree-view [directories]="directories"></ul>',
directives: [TreeViewComponent]
})
export class AppComponent {
directories: MenuItem[];
constructor() {
this.directories = [
{
"title": "Item-1",
"iconClass": "fa fa fa-flask",
"link": "#1",
"notice": 0,
"subMenus": null
},
{
"title": "Item-2", "iconClass": "fa fa-level-down", "link": null, "notice": 0,
"subMenus": [
{
"title": "Item-2-1",
"iconClass": "fa fa fa-flask",
"link": "#2",
"notice": 0,
"subMenus": null
}, {
"title": "Item-2-2",
"iconClass": "fa fa fa-flask",
"link": "#3",
"notice": 0,
"subMenus": null
}]
},
{
"title": "Item-3", "iconClass": "fa fa-level-down", "link": null, "notice": 4,
"subMenus": [
{
"title": "Item-3-1",
"iconClass": "fa fa fa-flask",
"link": "#4",
"notice": 1,
"subMenus": null
},
{
"title": "Item-3-2",
"iconClass": "fa fa fa-flask",
"link": null,
"notice": 3,
"subMenus": [
{
"title": "Item-3-2-1",
"iconClass": "fa fa fa-flask",
"link": "#6",
"notice": 1,
"subMenus": null
},
{
"title": "Item-3-2-2",
"iconClass": "fa fa fa-flask",
"link": "#7",
"notice": 2,
"subMenus": [
{
"title": "Item-4-2-1",
"iconClass": "fa fa fa-flask",
"link": "#6",
"notice": 1,
"subMenus": null
},
{
"title": "Item-4-2-2",
"iconClass": "fa fa fa-flask",
"link": "#7",
"notice": 2,
"subMenus": [
{
"title": "Item-5-2-1",
"iconClass": "fa fa fa-flask",
"link": "#6",
"notice": 1,
"subMenus": null
},
{
"title": "Item-5-2-2",
"iconClass": "fa fa fa-flask",
"link": "#7",
"notice": 2,
"subMenus": null
}]
}]
}]
}]
}
];
}
}
这里有人也许有人会问 directives 指令描述了 TreeViewComponent 组件,为什么我的 template 里面没提供的 <tree-view></tree-view>之类的自定义标签。细心的朋友会发现 ul 里有 tree-view 。没错,这事Angular2的另一种组件选择方式写法。如果采用自定义标签的方式,那么在原有的样式中,可能因为代码中多了<tree-view></tree-view> 会导致原来的样式失效了。
例如:
<style>
div > ul > li {
color: #0094ff;
} ...
</style>
<div>
<ul>
<li>
...
</li>
</ul>
</div> <!-- 加入 <tree-view></tree-view> 后 --> <div>
<tree-view>
<ul>
<li>
...
</li>
</ul>
</tree-view>
</div>
app/treeview/treevieww.component.ts代码:
import { Component, OnInit, Input } from '@angular/core';
import {MenuItem} from './menuItem';
@Component({
moduleId: module.id,
selector: '[tree-view]',
templateUrl: 'treeview.component.html',
directives: [TreeViewComponent],
})
export class TreeViewComponent implements OnInit {
@Input() directories: MenuItem[];
constructor() { }
ngOnInit() { }
}
使用自定义标签的选择方式,那么selector 选择器就不需要加上 [ ... ] 中括号。
最后结果:

oschina源码:点击这里
angular2 递归导航菜单实现方式的更多相关文章
- 使用像AdminLTE的前端框架,树形导航菜单实现方式都有哪些?
之前用easyui等富前端框架开发的时候都是使用封装好的县城的插件,现在使用最新的类似AdminLTE似的前段框架实现树形菜单都用什么方式? 后台拼接html然后前端用JS append方法添加还是直 ...
- vue+element UI递归方式实现多级导航菜单
介绍 这是一个是基于element-UI的导航菜单组件基础上,进行了二次封装的菜单组件,该组件以组件递归的方式,实现了可根据从后端接收到的json菜单数据,动态渲染多级菜单的功能. 使用方法 由于该组 ...
- vue+element UI以组件递归方式实现多级导航菜单
介绍 这是一个是基于element-UI的导航菜单组件基础上,进行了二次封装的菜单组件,该组件以组件递归的方式,实现了可根据从后端接收到的json菜单数据,动态渲染多级菜单的功能. 使用方法 由于该组 ...
- html自定义垂直导航菜单(多级导航菜单,去掉font-awesome图标,添加自己的箭头图标)
这次在原先html自定义垂直导航菜单的基础上做了比较大的改动: 1.去掉了font-awesome图标,上级菜单右边的箭头是自己用css写的,具体参考<css三角箭头>. 2.去掉了初始化 ...
- 前端框架bootstrap 表单和导航菜单的 Demo(第二篇)
表单: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...
- 微信5.4安卓版重回ios风格 导航菜单都放底栏位置
微信5.4安卓版发布更新了,由于本人的手机设置软件自动更新,中午的时候才发现微信换成了5.4版本,启动微信后是一个大大的“转账,就是发消息”,进入微信界面有点小惊喜,导航菜单都改为底部tab方式,顶部 ...
- 使用jQuery开发iOS风格的页面导航菜单
在线演示1 本地下载 申请达人,去除赞助商链接 iOS风格的操作系统和导航方式现在越来越流行,在今天的jQuery教程中,我们将介绍如何生成一个iphone风格的菜单导航. HTML代码 我们 ...
- SharePoint开发 - 自定义导航菜单(一)菜单声明与配置
博客地址 http://blog.csdn.net/foxdave 本篇描述自定义sharepoint菜单的一种方式,自定义菜单适用于一些门户等需求的网站 自定义的菜单有自己的数据源,可以是数据表,可 ...
- 使用CSS创建有图标的网站导航菜单
在我创建的每一个互联网应用中,我都试图避免创建完全由图片组成的菜单.在我看来,网页菜单系统中应该使用文字.这样做也会让菜单变得更干净利落.清晰和易读,不用考虑应用程序如何读取它,以及页面放大的时候也不 ...
随机推荐
- redis集群安装
1.普通安装 安装环境 centos 6.8 1.安装必要包 yum install gcc yum -y install wget 2.下载解压 wget http://download.redis ...
- Unity3d利用opencv保存游戏视频
脚本MyVideoWriter.cs using UnityEngine; using System.Collections; using OpenCvSharp; using OpenCvSharp ...
- Bootstrap插件系列——Bootstrap-table初始化、分页、客户端搜索、服务端搜索
又好久不写博客,最近项目都是用的bootstrap的样式,不出意外,应该是要在bootstrap的道路上越走越远了,所以下定决心,把bootstrap的插件都好好学学. 昨天写了boostrap-ta ...
- iOS.ReactNative-3-about-viewmanager-uimanager-and-bridgemodule
RCTViewManager and RCTUIManager 1. RCTViewManager 1.1 RCTViewManager 实现了接口RCTBridgeModule @interface ...
- Asp.Net MVC4入门指南(6):验证编辑方法和编辑视图
在本节中,您将开始修改为电影控制器所新加的操作方法和视图.然后,您将添加一个自定义的搜索页. 在浏览器地址栏里追加/Movies, 浏览到Movies页面.并进入编辑(Edit)页面. Edit(编辑 ...
- NopCommerce 框架系列(一)
今天,终于抽出时间来写写博文,也希望自己能养成写博文的好习惯,大神勿喷. 我从NopCommerce官网上下载了源码,以便自己学习研究,如有需要下载源码的朋友,请点击链接: http://www.no ...
- 您试图在此 Web 服务器上访问的 Web 应用程序当前不可用
错误提示: 服务器应用程序不可用 您试图在此 Web 服务器上访问的 Web 应用程序当前不可用.请点击 Web 浏览器中的“刷新”按钮重试您的请求. 管理员注意事项: 详述此特定请求失败原因的错误信 ...
- Reveal分析IOS界面,plist文件读取
Reveal分析IOS界面,需要得到app的 softwareVersionBundleId上传到iphone中 , 而IOS8的iTunesMetadata.plist (设备路径/var/mobi ...
- 32位Windows 7系统下,显示4G内存3G可用的原因。
由于32位操作系统只有那么多物理地址可用, 而硬盘.光驱.声卡,显卡,无线网卡等硬件设备也需要分配物理地址才可以使用, 所以系统会把一部分物理地址分配给它们, 剩下的物理地址分配给内存使用, 而剩下的 ...
- Android数据库安全解决方案,使用SQLCipher进行加解密
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952409 我们都知道,Android系统内置了SQLite数据库,并且提供了一 ...