有时候,自定义的Directive中需要调用controller中的方法,即Directive与controller有一定的耦合度。

比如有如下的一个controller:

app.controller('MyCtrl',function($scope){
$scope.load = function(){
console.log('loading more...')
}
});

现在自定义一个Direcitve,需要调用MyCtrl这个controller中的load方法。

app.directive('enter', function(){
return function($scope, ele, attrs){
ele.bind('mouseenter', function(){
$scope.load();
})
}
})

页面这样调用:

 <div ng-controller="MyCtrl">
<div enter>enter to load more</div>
</div>

如果想调用MyCtrl中的其它方法呢?这时候就需要更改direcitve中的编码。由此可见在directive以硬编码的方法是调用controller中的方法是不太合理的。

那么把变化的可能性放在页面上会怎样呢?

给enter一个属性值,该属性值表示调用哪个方法。

  <div ng-controller="MyCtrl">
<div enter="load()">enter to load more</div>
</div>

这样,总比在directive中硬编码要灵活一些。

directive相应改成这样:

app.directive('enter', function(){
return function($scope, ele, attrs){
ele.bind('mouseenter', function(){
$scope.$apply('load()');
})
}
})

可是,以上写法还不是最合理的,因为在调用上下文的$apply方法的时候传入的实参也是字符串。

别忘了,link函数中还有一个形参attrs,通过这个可以获取任意属性值。

app.directive('enter', function(){
return function($scope, ele, attrs){
ele.bind('mouseenter', function(){
$scope.$apply(attrs.enter);
})
}
})

AngularJS自定义Directive与controller的交互的更多相关文章

  1. AngularJS: 自定义指令与控制器数据交互

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. angularJS中directive与controller之间的通信

    当我们在angularJS中自定义了directive之后需要和controller进行通讯的时候,是怎么样进行通讯呢? 这里介绍3种angular自定义directive与controller通信的 ...

  3. AngularJS自定义Directive中link和controller的区别

    在AngularJS中,自定义Directive过程中,有时用link和controller都能实现相同的功能.那么,两者有什么区别呢? 使用link函数的Directive 页面大致是: <b ...

  4. AngularJS自定义Directive不一定返回对象

    AngularJS中,当需要自定义Directive时,通常返回一个对象,就像如下的写法: angular.module('modulename') .directive('myDirective', ...

  5. AngularJS自定义Directive初体验

    通常我们这样定义个module并随之定义一个controller. var app = angular.module('myApp', []); app.controller('CustomersCo ...

  6. AngularJS自定义Directive

    (编辑完这篇之后,发现本篇内容应该属于AngularJS的进阶,内容有点多,有几个例子偷懒直接用了官方的Demo稍加了一些注释,敬请见谅). 前面一篇介绍了各种常用的AngularJS内建的Direc ...

  7. 【转载】AngularJs 指令directive之controller,link,compile

    关于自定义指令的命名,你可以随便怎么起名字都行,官方是推荐用[命名空间-指令名称]这样的方式,像ng-controller.不过你可千万不要用 ng-前缀了,防止与系统自带的指令重名.另外一个需知道的 ...

  8. 49.AngularJs 指令directive之controller,link,compile

    转自:https://www.cnblogs.com/best/tag/Angular/ 关于自定义指令的命名,你可以随便怎么起名字都行,官方是推荐用[命名空间-指令名称]这样的方式,像ng-cont ...

  9. angularJs中自定义directive的数据交互

    首先放官方文档地址:https://docs.angularjs.org/guide/directive 就我对directive的粗浅理解,它一般用于独立Dom元素的封装,应用场合为控件重用和逻辑模 ...

随机推荐

  1. Bug Bounty Reference

    https://github.com/ngalongc/bug-bounty-reference/blob/master/README.md#remote-code-execution Bug Bou ...

  2. 终极利器!利用appium和mitmproxy登录获取cookies

    环境搭建 参考我之前写的https://www.cnblogs.com/c-x-a/p/9163221.html appium 代码start_appium.py # -*- coding: utf- ...

  3. 经典sql-获取当前文章的上一篇和下一篇

    我们在做资讯类的网站的时候,肯定会有这么一个需求,就是在资讯内容页的下方需要给出上一篇和下一篇资讯的链接.上次我一同事兼好友兼室友就遇到了这么一个需求,一开始我们都把问题想复杂了,先取的是符合条件的资 ...

  4. PHP实现 APP端微信支付功能

    1.我封装好的一个支付类文件,多余的东西都去除掉了,并且把配置参数放到了这个支付类中,只需要修改Weixinpayandroid方法内的几个参数就可以直接复制使用: class Wxpayandroi ...

  5. 【转】卖萌的大牛你桑不起啊 ——记CVPR2011一篇极品文章

    来源:http://blog.renren.com/share/228707015/7197269922 作者 : 庞宇 CVPR2011正在如火如荼的进行中,在网上能看到的部分文章中,我终于找到一篇 ...

  6. ***Bootstrap FileInput插件的使用经验汇总

    插件下载地址: https://github.com/kartik-v/bootstrap-fileinput/ 官方DEMO查看: http://plugins.krajee.com/file-ba ...

  7. Nginx+redis的Asp.net

    基于Nginx+redis的Asp.net站点搭建   剧情介绍 在传统的信息系统(比如小规模的ERP\MES系统),往往只是进行简单的应用服务器和数据库服务器的分布式部署,以此来提高应用系统的负载能 ...

  8. 【AtCoder】Dwango Programming Contest V题解

    A - Thumbnail 题意简述:给出N个数,找出N个数中和这N个数平均值绝对值最小的数 根据题意写代码即可= = #include <bits/stdc++.h> #define f ...

  9. 【noip模拟赛3】拣钱

    描述 最近,Henry由于失恋(被某大牛甩掉!)心情很是郁闷.所以,他去了大牛家,寻求Michael大牛的帮助,让他尽快从失恋的痛苦中解脱出来.Michael大牛知道Henry是很爱钱的,所以他是费尽 ...

  10. PowerShell 中使用 mvn 编译报错 Unknown lifecycle phase ".test.skip=true". 解决办法

    nknown lifecycle phase “–Dmaven.test.skip=true”. You must specify a valid lifecycle phase or a goal ...