国产探花免费观看_亚洲丰满少妇自慰呻吟_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制作一個菜單的相關知識,希望對大家有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中超| 永昌县| 玉林市| 防城港市| 鹤峰县| 定远县| 通州市| 弋阳县| 屯留县| 颍上县| 洛阳市| 田林县| 北安市| 德保县| 娱乐| 义马市| 子长县| 土默特左旗| 讷河市| 鹰潭市| 江达县| 青州市| 青阳县| 玉山县| 昭通市| 平泉县| 普洱| 卢氏县| 开原市| 建昌县| 连城县| 蒙城县| 灵寿县| 九江县| 定襄县| 彝良县| 灵石县| 金寨县| 内江市| 台州市| 揭西县|