This article introduces how to create a menu in Directive in AngualrJS, which involves angularjsdireve VE related knowledge. This article introduces the menu in great detail and has reference value. I would like to share with you how to write menus frequently:
Whether the menu item is highlighted depends on the highlight method in the controller.
vm.highlight = funciton(path){return $locaiton.path().substr(0, path.lenght) === path;}
If you use direve VE, it will be simpler.
- Customers
- Customers
- Customers
Directive is roughly:
(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){//Customersvar 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);}());
The above content is about how to create a menu for Directive in AngualrJS, and I hope to help you.