Angluar+zorro實(shí)現(xiàn)無限級(jí)菜單
關(guān)于Angular + zorro 實(shí)現(xiàn)無限級(jí)菜單,供大家參考,具體內(nèi)容如下
該文章為思路+代碼,為通用式前端無限級(jí)菜單。
首先通過后臺(tái)接收到的數(shù)據(jù)是這樣的
"table":[ ? ? { ? ? "id": 1017.0, ? ? "menuName": "用戶管理", ? ? ? ? "child":[{ ? ? ? ? ? ? "id": 23.0, ? ? ? ? ? ? "menuName": "用戶維護(hù)", ? ? ? ? ? ? "child": [{ ? ? ? ? ? ? ? ? ? ? "id": 24.0, ? ? ? ? ? ? ?? ??? ?"menuName": "用戶查看", ? ? ? ? ? ? ? ? ? ? "child":[{ ? ? ? ? ? ? ? ? ? ??? ??? ? ? ?"id": 25.0, ? ? ? ? ? ? ?? ??? ??? ??? ?"menuName": "用戶增加", ? ? ? ? ? ? ? ? ? ??? ??? ??? ?"child":[] ? ? ? ? ? ? ? ? ? ? }] ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? "id": 25.0, ? ? ? ? ? ? ?? ??? ?"menuName": "用戶增加", ? ? ? ? ? ? ? ? ? ? "child":[] ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ] ? ? ? ? }] ? ? }, ? ? { ? ? "id": 1018.0, ? ? "menuName": "微信管理", ? ? "child":[] ? ? ? ? } ]
實(shí)現(xiàn)步驟如下:
1. uc-home.component.html
<!-- strHtml : 需要展示的數(shù)據(jù) innerhtmlpipe :添加管道,讓數(shù)據(jù)擁有樣式 --> <div [innerHTML]="strHtml | innerhtmlpipe"></div>
2. 因?yàn)橥ㄟ^在component.ts進(jìn)行數(shù)據(jù)拼接傳到頁面樣式不會(huì)顯示,所以使用Angular提供的管道讓其有樣式。
2.1新建一個(gè)管道
命令: ng g pipe innerhtmlpipePipe
2.2.
innerhtmlpipePipe.pipe.ts
import { Pipe, PipeTransform } from '@angular/core'; import {DomSanitizer} from '@angular/platform-browser'; @Pipe({ ? name: 'innerhtmlpipe' }) export class InnerhtmlpipePipe implements PipeTransform { constructor(private sanitizer: DomSanitizer) {} ? transform(value): any { ? ? ? //樣式 ? ? return this.sanitizer.bypassSecurityTrustHtml(value); ? } }
3.uc-home.component.ts
import {Component, OnInit, ChangeDetectorRef} from '@angular/core'; import {Router, NavigationEnd} from '@angular/router'; @Component({ ? selector: 'nb-uc-home', ? templateUrl: './uc-home.component.html', ? styleUrls: ['./uc-home.component.scss'], ? animations: [slideInAnimation] }) export class UcHomeComponent implements OnInit { ? ? //定義一個(gè) strHtml ? ? public ?strHtml; ? ? //數(shù)據(jù) ? ? public menuArray = []; ? ?? ? ? ?constructor( ? ? ??? ?private homeService: HomeService, ? ? ? ? ?private ref: ChangeDetectorRef ? ? ?) {} ? ? ngOnInit() { ? ? ? ? ? ?//從后臺(tái)接口獲取數(shù)據(jù),賦值給menuArray ? ?this.homeService.getMenu().subscribe(data => { ? ? ? ?//賦值數(shù)據(jù) ? ? ? ? this.menuArray = data.table; ? ? ? ? ? ?//初始化頁面 ? ? ? ? this.strHtml = ''; ? ? ? ?//遍歷每一個(gè)數(shù)據(jù) ? for (let i = 0; i < this.menuArray.length; i++) { ? ? ?? ? ? ? ? const arr = this.menuArray[i]; //開始拼接頁面 ? ? ? ? this.strHtml += '<ul nz-menu [nzMode]="\'inline\'" style="height:auto" >'; ? ? ? ?? ?this.strHtml +='<li nz-submenu>'; ? ? ? ? this.strHtml += '<div menuEvent title>'; ? ? ? ? this.strHtml +='<span type="laptop">' + arr.menuName + '</span>' ; ? ? ? ?? ?this.strHtml +='</div>'; ? ? ? //遍歷到孩子的時(shí)候調(diào)用一個(gè)方法,循環(huán)把孩子全部遍歷出來 ? ? ? ? this.strHtml += this.creatHtml(arr.child); ? ? ? ? this.strHtml += '</li>'; ? ? ? ?? ?this.strHtml += '</ul>'; ? ? ? } ? ? ? ? //數(shù)據(jù)加載完畢之后重新渲染頁面 ? ? ? ? ? ? ?this.ref.markForCheck(); ? ? ? }); ? ? ? ? ? } ? ?? ?creatHtml(cArr): any { ? ? let str = ''; ? ? for (let i = 0; i < cArr.length; i++) { ? ? ? str += '<ul>'; ? ? ? str += '<li nz-menu-item'; ? ? ? str += '<div menuEvent>'; ? ? ? str += '<span>' + cArr[i].menuName +'</span>'; ? ? ? str += '</div>'; ? ? ? str += '</li>'; ? ? ? str += '</ul>'; ? ? } ? ? ?//是否存在子集 ? ? if (cArr.child) { ? ? ? str += this.creatHtml(cArr.child); ? ? } ? ? this.ref.markForCheck(); ? ? return str; ? } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- AngularJS實(shí)現(xiàn)樹形結(jié)構(gòu)(ztree)菜單示例代碼
- 實(shí)例詳解AngularJS實(shí)現(xiàn)無限級(jí)聯(lián)動(dòng)菜單
- AngularJS折疊菜單實(shí)現(xiàn)方法示例
- AngularJS實(shí)現(xiàn)的select二級(jí)聯(lián)動(dòng)下拉菜單功能示例
- Angular.js與Bootstrap相結(jié)合實(shí)現(xiàn)手風(fēng)琴菜單代碼
- AngularJS動(dòng)態(tài)菜單操作指令
- AngularJS+Bootstrap3多級(jí)導(dǎo)航菜單的實(shí)現(xiàn)代碼
- AngularJS模糊查詢功能實(shí)現(xiàn)代碼(過濾內(nèi)容下拉菜單排序過濾敏感字符驗(yàn)證判斷后添加表格信息)
- Angular.JS實(shí)現(xiàn)無限級(jí)的聯(lián)動(dòng)菜單(使用demo)
- angularjs+bootstrap菜單的使用示例代碼
相關(guān)文章
AngularJS輕松實(shí)現(xiàn)雙擊排序的功能
網(wǎng)上已經(jīng)有AngularJS比較多的相關(guān)教程了,那么這篇文章我們一起來看一個(gè)AngularJS雙擊排序的例子,對(duì)大家日常開發(fā)很有幫助的,有需要的可以參考借鑒。2016-08-08詳解AngularJS如何實(shí)現(xiàn)跨域請(qǐng)求
跨域請(qǐng)求一直是網(wǎng)頁編程中的一個(gè)難題,在過去,絕大多數(shù)人都傾向于使用JSONP來解決這一問題。不過現(xiàn)在,我們可以考慮一下W3C中一項(xiàng)新的特性——CORS(Cross-Origin Resource Sharing)了。2016-08-08Angularjs中數(shù)據(jù)綁定的實(shí)例詳解
這篇文章主要介紹了Angularjs中數(shù)據(jù)綁定的實(shí)例詳解的相關(guān)資料,這里提供簡單實(shí)例,大家可以參考下,希望通過本文可以掌握這部分內(nèi)容,需要的朋友可以參考下2017-08-08angularjs數(shù)組判斷是否含有某個(gè)元素的實(shí)例
下面小編就為大家分享一篇angularjs數(shù)組判斷是否含有某個(gè)元素的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02