国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

AngualrJS中的Directive制作一個菜單

2019-11-20 10:41:06
字體:
來源:轉載
供稿:網友

說下我經常寫菜單的方式:

<ul><li data-ng-class="{'active': highlight('/orders')}"><a href="#/orders">Orders</a></li></ul> 

菜單項是否高亮顯示取決于controller中的highlight方法。

vm.highlight = funciton(path){return $locaiton.path().substr(0, path.lenght) === path;}

如果以Directive的方式會更簡潔。

<ul menu-highlighter highlight-class-name="active"><li><a href="#/customers">Customers</a></li><li><a href="#/orders">Customers</a></li><li><a href="#/about">Customers</a></li></ul> 

Directive大致是:

(function(){var injectParams = ['$location'];var menuHighlighter = function($location){var link = function(scope, element){function setActive(){var path = $location.path();var className = scope.highlightClassName || 'active';if(path){angular.forEac(element.find('li'), function(li){//<a href="#/customers">Customers</a>var anchor = li.querySelector('a');//#/customersvar href=(anchor && anchor.href) ? anchor.href : anchor.getAttribute('data-href').replace('#','');//customersvar trimmedHref = href.substr(href.indexOf('#/')+1, href.length);var basePath = path.substr(0, trimmedHref.length);if(trimmedHref === basePath){angular.element(li).addClass(className);} else {angular.element(li).removeClass(className);}});} }setActive();scope.$on('$locationChangeSuccess', setActive);};return {restrict: 'A',scope: {highlightClassName: '@'},link: link}};menuHighlighter.$inject = injectParams;angular.module('my.directives').directive('menuHighlighter', menuHighlighter);}()); 

以上內容是針對AngualrJS中的Directive制作一個菜單的相關知識,希望對大家有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 册亨县| 连州市| 自贡市| 许昌县| 翼城县| 石门县| 宿迁市| 平度市| 南宁市| 钟山县| 江口县| 灵山县| 洛宁县| 望江县| 常州市| 西乡县| 大悟县| 龙口市| 西乡县| 甘南县| 隆昌县| 阳泉市| 开封县| 蓝山县| 秀山| 祁门县| 平邑县| 田东县| 宜章县| 诸城市| 繁昌县| 长葛市| 福鼎市| 万载县| 上虞市| 肃宁县| 柳州市| 黄龙县| 乐清市| 屯昌县| 曲麻莱县|