Angularjs中使用指令绑定点击事件的方法


Posted in Javascript onMarch 30, 2017

项目中,模板中的菜单是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");
      });
    }
  }
});

原来指令是这样使用的。以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript Demo模态窗口
Dec 06 Javascript
关于jQuery中的end()使用方法
Jul 10 Javascript
js中函数声明与函数表达式
Jun 03 Javascript
JavaScript计算某一天是星期几的方法
Aug 05 Javascript
jquery对Json的各种遍历方法总结(必看篇)
Sep 29 Javascript
javascript 中Cookie读、写与删除操作
Mar 29 Javascript
利用require.js与angular搭建spa应用的方法实例
Jul 19 Javascript
Vue2.X 通过AJAX动态更新数据
Jul 17 Javascript
在layer弹层layer.prompt中,修改placeholder的实现方法
Sep 27 Javascript
Angular如何由模板生成DOM树的方法
Dec 23 Javascript
Vue数字输入框组件使用方法详解
Feb 10 Javascript
Vite和Vue CLI的优劣
Jan 30 Vue.js
Angualrjs和bootstrap相结合实现数据表格table
Mar 30 #Javascript
微信小程序中顶部导航栏的实现代码
Mar 30 #Javascript
ES6中Proxy与Reflect实现重载(overload)的方法
Mar 30 #Javascript
vue分类筛选filter方法简单实例
Mar 30 #Javascript
教你快速搭建Node.Js服务器的方法教程
Mar 30 #Javascript
Vuejs仿网易云音乐实现听歌及搜索功能
Mar 30 #Javascript
canvas绘制一个常用的emoji表情
Mar 30 #Javascript
You might like
php 不使用js实现页面跳转
2014/02/11 PHP
php curl抓取网页的介绍和推广及使用CURL抓取淘宝页面集成方法
2015/11/30 PHP
分享一个漂亮的php验证码类
2016/09/29 PHP
几行代码轻松实现PHP文件打包下载zip
2017/03/01 PHP
用htc组件制作windows选项卡
2007/01/13 Javascript
jquery实现鼠标悬浮停止轮播特效
2020/08/20 Javascript
js针对ip地址、子网掩码、网关的逻辑性判断
2016/01/06 Javascript
详解Javascript中的Object对象
2016/02/28 Javascript
BootStrap中
2016/12/10 Javascript
Bootstrap 模态框实例插件案例分析
2016/12/28 Javascript
js模拟支付宝密码输入框
2017/04/11 Javascript
vue 中滚动条始终定位在底部的方法
2018/09/03 Javascript
node解析修改nginx配置文件操作实例分析
2019/11/06 Javascript
Vue路由 重定向和别名的区别说明
2020/09/09 Javascript
[03:02]2014DOTA2西雅图邀请赛 让队员自己告诉你DK NAVI备战情况
2014/07/08 DOTA
python基础教程之popen函数操作其它程序的输入和输出示例
2014/02/10 Python
用Python进行基础的函数式编程的教程
2015/03/31 Python
Python中利用sqrt()方法进行平方根计算的教程
2015/05/15 Python
Python的collections模块中的OrderedDict有序字典
2016/07/07 Python
Python文件循环写入行时防止覆盖的解决方法
2018/11/09 Python
python使用udp实现聊天器功能
2018/12/10 Python
TensorFlow加载模型时出错的解决方式
2020/02/06 Python
pycharm远程连接vagrant虚拟机中mariadb数据库
2020/06/05 Python
keras读取h5文件load_weights、load代码操作
2020/06/12 Python
python中判断文件结束符的具体方法
2020/08/04 Python
html5实现完美兼容各大浏览器的播放器
2014/12/26 HTML / CSS
英国马匹装备和马术用品购物网站:Equine Superstore
2019/03/03 全球购物
Internet主要有哪些网络群组成
2015/12/24 面试题
2015年幼儿园毕业感言
2014/02/12 职场文书
超市促销活动总结
2014/07/01 职场文书
2014年人力资源部工作总结
2014/11/19 职场文书
出国签证在职证明范本
2014/11/24 职场文书
先进班集体申报材料
2014/12/26 职场文书
清洁员岗位职责
2015/02/15 职场文书
python的变量和简单数字类型详解
2021/09/15 Python
Win11右下角图标点了没反应怎么办?Win11点击右下角图标无反应解决方法汇总
2022/07/07 数码科技