看了网上很多源码,基本都是采用循环三级的方式。如果是无限级的菜单,就无法实现了。

菜单格式:

 [
{
"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 递归导航菜单实现方式的更多相关文章

  1. 使用像AdminLTE的前端框架,树形导航菜单实现方式都有哪些?

    之前用easyui等富前端框架开发的时候都是使用封装好的县城的插件,现在使用最新的类似AdminLTE似的前段框架实现树形菜单都用什么方式? 后台拼接html然后前端用JS append方法添加还是直 ...

  2. vue+element UI递归方式实现多级导航菜单

    介绍 这是一个是基于element-UI的导航菜单组件基础上,进行了二次封装的菜单组件,该组件以组件递归的方式,实现了可根据从后端接收到的json菜单数据,动态渲染多级菜单的功能. 使用方法 由于该组 ...

  3. vue+element UI以组件递归方式实现多级导航菜单

    介绍 这是一个是基于element-UI的导航菜单组件基础上,进行了二次封装的菜单组件,该组件以组件递归的方式,实现了可根据从后端接收到的json菜单数据,动态渲染多级菜单的功能. 使用方法 由于该组 ...

  4. html自定义垂直导航菜单(多级导航菜单,去掉font-awesome图标,添加自己的箭头图标)

    这次在原先html自定义垂直导航菜单的基础上做了比较大的改动: 1.去掉了font-awesome图标,上级菜单右边的箭头是自己用css写的,具体参考<css三角箭头>. 2.去掉了初始化 ...

  5. 前端框架bootstrap 表单和导航菜单的 Demo(第二篇)

    表单: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...

  6. 微信5.4安卓版重回ios风格 导航菜单都放底栏位置

    微信5.4安卓版发布更新了,由于本人的手机设置软件自动更新,中午的时候才发现微信换成了5.4版本,启动微信后是一个大大的“转账,就是发消息”,进入微信界面有点小惊喜,导航菜单都改为底部tab方式,顶部 ...

  7. 使用jQuery开发iOS风格的页面导航菜单

    在线演示1 本地下载     申请达人,去除赞助商链接 iOS风格的操作系统和导航方式现在越来越流行,在今天的jQuery教程中,我们将介绍如何生成一个iphone风格的菜单导航. HTML代码 我们 ...

  8. SharePoint开发 - 自定义导航菜单(一)菜单声明与配置

    博客地址 http://blog.csdn.net/foxdave 本篇描述自定义sharepoint菜单的一种方式,自定义菜单适用于一些门户等需求的网站 自定义的菜单有自己的数据源,可以是数据表,可 ...

  9. 使用CSS创建有图标的网站导航菜单

    在我创建的每一个互联网应用中,我都试图避免创建完全由图片组成的菜单.在我看来,网页菜单系统中应该使用文字.这样做也会让菜单变得更干净利落.清晰和易读,不用考虑应用程序如何读取它,以及页面放大的时候也不 ...

随机推荐

  1. as3自定义事件

    package EventPackage { import flash.events.Event; /** * * @author tqr <br /> * 创建时间:2015-2-6 下 ...

  2. String和string的区别(C#)

    从位置讲: 1.String是.NET  Framework里面的String,小写的string是C#语言中的string 2.如果把using System;删掉,没有大写的String了,Sys ...

  3. android ListView点击item返回后listview滚动位置

    1.Don't work when dynamically loading content Parcelable state; @Override public void onPause() { // ...

  4. ng2收获

    1.devDependencies下只有在开发应用时才用得到这个我是知道的. 但是我不知道的事要想达到这个效果是要在生产环境安装包的时候必须要加个这个才行"--production" ...

  5. hibernate基础

    另存为查看吧

  6. httpServletRequest对象、filter、servlet、servlet容器、catalina、tomcat、以及web容器之间的关系

    学习servlet的时候经常感到疑惑 HttpServletRequest是服务器创建的?还是servlet容器创建的? 过滤器是服务器创建的?还是servlet容器创建的? serlet容器和tom ...

  7. SpringMVC 400 Bad Request 问题

    摘要 SpringMVC 400 Bad Request 在提交表单时,发生400错误,并未进入save方法. @RequestMapping(value="/!save",met ...

  8. java基于socket公共聊天室的实现

    项目:一个公共聊天室功能的实现,实现了登录聊天,保存聊天记录等功能. 一.实现代码 1.客户端 ChatClient.java import java.io.BufferedReader; impor ...

  9. OpenMP并行构造的schedule子句详解 (转载)

    原文:http://blog.csdn.net/gengshenghong/article/details/7000979 schedule的语法为: schedule(kind, [chunk_si ...

  10. java 中与 或 非 异或 和位移运算

    与(&) 或(|) 异或(^) 和位移(>>,<<) 通常和符号位无关 .. 但是非比较特殊,与符号位有关,所以计算的时候要考虑符号位 先扩展为32字符,前16位为符号 ...