Angluar+zorro實(shí)現(xiàn)無(wú)限級(jí)菜單
關(guān)于Angular + zorro 實(shí)現(xiàn)無(wú)限級(jí)菜單,供大家參考,具體內(nèi)容如下
該文章為思路+代碼,為通用式前端無(wú)限級(jí)菜單。
首先通過(guò)后臺(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)橥ㄟ^(guò)在component.ts進(jìn)行數(shù)據(jù)拼接傳到頁(yè)面樣式不會(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;
? ? ? ? ? ?//初始化頁(yè)面
? ? ? ? this.strHtml = '';
? ? ? ?//遍歷每一個(gè)數(shù)據(jù)
? for (let i = 0; i < this.menuArray.length; i++) {
? ? ??
? ? ? ? const arr = this.menuArray[i];
//開(kāi)始拼接頁(yè)面
? ? ? ? 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)把孩子全部遍歷出來(lái)
? ? ? ? this.strHtml += this.creatHtml(arr.child);
? ? ? ? this.strHtml += '</li>';
? ? ? ?? ?this.strHtml += '</ul>';
? ? ? }
? ? ? ? //數(shù)據(jù)加載完畢之后重新渲染頁(yè)面
? ? ? ? ? ? ?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;
? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- AngularJS實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)(ztree)菜單示例代碼
- 實(shí)例詳解AngularJS實(shí)現(xiàn)無(wú)限級(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)代碼(過(guò)濾內(nèi)容下拉菜單排序過(guò)濾敏感字符驗(yàn)證判斷后添加表格信息)
- Angular.JS實(shí)現(xiàn)無(wú)限級(jí)的聯(lián)動(dòng)菜單(使用demo)
- angularjs+bootstrap菜單的使用示例代碼
相關(guān)文章
Angularjs編寫(xiě)KindEditor,UEidtor,jQuery指令
使用過(guò) AngularJS 的朋友應(yīng)該最感興趣的是它的指令?,F(xiàn)今市場(chǎng)上的前端框架也只有AngularJS 擁有自定義指令的功能,并且AngularJS 是目前唯一提供Web應(yīng)用可復(fù)用能力的框架。2015-01-01
AngularJS中使用three.js的實(shí)例詳解
這篇文章主要介紹了AngularJS中使用three.js的實(shí)例詳解,我將之前自己做的demo放到了angularJS的一個(gè)component中,其實(shí)一開(kāi)始是沒(méi)有準(zhǔn)備用框架的但是后面發(fā)現(xiàn)需要進(jìn)行的雙向綁定越來(lái)越多,后期表單數(shù)據(jù)的變化量也很大,最后還是選擇用NG來(lái)做這些事情2017-07-07
如何在Angular8.0下使用ngx-translate進(jìn)行國(guó)際化配置
這篇文章主要介紹了如何在Angular8.0下使用ngx-translate進(jìn)行國(guó)際化配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
AngularJS中的指令實(shí)踐開(kāi)發(fā)指南(二)
這篇文章主要介紹了AngularJS中的指令實(shí)踐指南(二)的相關(guān)資料,需要的朋友可以參考下2016-03-03
angularjs 表單密碼驗(yàn)證自定義指令實(shí)現(xiàn)代碼
這篇文章主要介紹了angularjs 表單密碼驗(yàn)證自定義指令實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-10-10
Angular設(shè)置title信息解決SEO方面存在問(wèn)題
爬蟲(chóng)在檢索seo信息的時(shí)候會(huì)讀不了js給其賦的值,導(dǎo)致搜索引擎收錄不了或者收錄了無(wú)效的信息,下面本文給大家介紹Angular設(shè)置title信息解決SEO方面存在問(wèn)題,需要的朋友可以參考下2016-08-08
Angular應(yīng)用tsconfig.json中的lib屬性示例解析
這篇文章主要介紹了Angular應(yīng)用tsconfig.json中的lib屬性示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

