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

菜单格式:

 [
{
"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. JavaScript数组的一些方法集合

    数组方法集合 push()添加到数组末尾,并返回修改后数组的长度 var a=array.push('a','b'); alert(a);//2 pop() 移除数组最后一项,返回移除的项. shif ...

  2. HYSBZ 4551 (树状数组) 采花

    题目:这里 题意: 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记, ...

  3. Apache htpasswd命令用法详解

    一. 基础 htpasswd建立和更新存储用户名.密码的文本文件, 用于对HTTP用户的basic认证. # /usr/local/apache/bin/htpasswd –help Usage: h ...

  4. WNDR3700V4 安装SVN Server

    下文所用路由器型号为:WNDR3700V4 参考链接:http://dd-wrt.ca/phpBB2/viewtopic.php?t=86912&highlight=optware http: ...

  5. python python 入门学习之网页数据爬虫cnbeta文章保存

    需求驱动学习的动力. 因为我们单位上不了外网所以读新闻是那么的痛苦,试着自己抓取网页保存下来,然后离线阅读.今天抓取的是cnbeta科技新闻,抓取地址是http://m.cnbeta.com/wap/ ...

  6. HttpWebResponse远程服务器返回错误: (500) 内部服务器错误。

    现象 我们编码实现请求一个页面时,请求的代码类似如下代码: HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl); req.Us ...

  7. C# System.Timers.Timer的一些小问题?

    比如设置间隔时间是 1000msSystem.Timers.Timer mytimer = new System.Timers.Timer(1000);问题若响应函数执行的时间超过了 1000 ms, ...

  8. UI Automation Test

    UI Automation test is based on the windows API. U can find the UI Automation MSDN file from http://m ...

  9. MVC模式下向qq邮箱发送邮件

    将已经保存在数据库中的密码通过邮件发送到qq邮箱中.用的ssm框架,其中的config文件要先配置好. 用到的jar包有gson-2.2.1.jar,gson.jar,mail.jar,activat ...

  10. js 字符串格式化方法

    String.prototype.format = function(args) { var result = this; if (arguments.length > 0) { if (arg ...