Angularjs中使用指令绑定点击事件的方法
程序员文章站
2022-07-05 21:11:57
项目中,模板中的菜单是jquery控制的,在angularjs中就运行不到了,因为菜单项是ng-repeat之后的。
如html
项目中,模板中的菜单是jquery控制的,在angularjs中就运行不到了,因为菜单项是ng-repeat之后的。
如html
<ul id="main-menu"> <li class=""> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >menu1</a> <ul class="sub-menu"> <li ><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >--1</a></li> <li ><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >--2</a></li> </ul> </li> <li class=""> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >menu2</a> <ul class="sub-menu"> <li ><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >--1</a></li> <li ><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >--2</a></li> </ul> </li> </ul>
jquery给第一级a链接绑定事件代码像:
$(function(){ $("#main-menu li a").click(function(e){ if ($(this).next().hasclass('sub-menu') === false) { return; } console.log("click"); }); });
因为我之前看过文档说,angularjs的controller不处理dom的操作,所以一直在找方法怎么处理和jquery 一样绑定a的点击事件,在看了jquery not working with ng-repeat results之后,原来可以将所有链接的单击事件,放在一个指令中。如果在controller中绑定了ng-click,并操作了dom元素,就不太规范了,使用指令会好一些。
html
<ul id="main-menu"> <li class=""> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" toggle-main-menu>menu1</a> <ul class="sub-menu"> <li ><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >--1</a></li> <li ><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >--2</a></li> </ul> </li> <li class=""> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" toggle-main-menu>menu2</a> <ul class="sub-menu"> <li ><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >--1</a></li> <li ><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >--2</a></li> </ul> </li> </ul>
javascript:
.directive("togglemainmenu", function() { return { restrict: "a", link: function(scope, elem, attrs) { $(elem).click(function() { if($(this).next().hasclass('sub-menu') === false) { return; } console.log("click"); }); } } });
原来指令是这样使用的。以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 将数组排成最小的数
推荐阅读
-
AngularJs中 ng-repeat指令中实现含有自定义指令的动态html的方法
-
Angularjs使用directive自定义指令实现attribute继承的方法详解
-
在layui中layer弹出层点击事件无效的解决方法
-
Vue.js 中的 v-model 指令及绑定表单元素的方法
-
AngularJS使用带属性值的ng-app指令实现自定义模块自动加载的方法
-
AngularJS中ng-options实现下拉列表的数据绑定方法
-
AngularJs中route的使用方法和配置
-
jQuery的三种bind/One/Live/On事件绑定使用方法
-
浅谈Vue.js 中的 v-on 事件指令的使用
-
angularjs中回车键触发某一事件的方法