AngualrJS中的Directive制作一个菜单
作者:bea
说下我经常写菜单的方式: <ul><li data-ng-class="{'active': highlight('/orders')}"><a href="#/orders">Orders</a></li></ul> 菜单项是否高亮显示取决于controller中的highlight方法。 vm.highlight = funciton(path){return $locaiton.pat
说下我经常写菜单的方式:
<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');
//#/customers
var href=(anchor && anchor.href) ? anchor.href : anchor.getAttribute('data-href').replace('#','');
//customers
var 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制作一个菜单的相关知识,希望对大家有所帮助。
有用 | 无用
<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');
//#/customers
var href=(anchor && anchor.href) ? anchor.href : anchor.getAttribute('data-href').replace('#','');
//customers
var 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制作一个菜单的相关知识,希望对大家有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- js实现的鼠标滚轮滚动切换页面效果(类似360默认页面滚动切换效果)
- AngularJS转换响应内容
- jQuery+css实现的切换图片功能代码
- javascript中的3种继承实现方法
- jQuery+css实现的换页标签栏效果
- js实现的彩色方块飞舞奇幻效果
- JavaScript下的时间格式处理函数Date.prototype.format
- 基于JavaScript实现瀑布流效果(循环渐近)
- jQuery Easyui学习之datagrid 动态添加、移除editor
- js实现简单排列组合的方法
- jQuery插件开发精品教程让你的jQuery提升一个台阶
- JavaScript+html5 canvas制作的圆中圆效果实例
- jQuery ajax分页插件实例代码
- JavaScript+html5 canvas绘制的小人效果
- jquery ajax分页插件的简单实现
- JavaScript+html5 canvas制作色彩斑斓的正方形效果
- APP中javascript+css3实现下拉刷新效果
- jQuery EasyUi实战教程之布局篇
- jQuery Easyui实现左右布局