“指令之之所以要定义成指令就是为了复用!”

指令一定是可以用在不同的controller里面的,为了在不同的controller去使用它,我们一定要给指定的配置项一个指令。这样才能跟外面的控制器进行交互。

举例如下:

html:

    <div ng-controller="MyCtrl">
<loader howToLoad="loadData()">滑动加载</loader>
</div>
<div ng-controller="MyCtrl2">
<loader howToLoad="loadData2()">滑动加载2</loader>
</div>

js:

var app = angular.module('MyModule',[]);

app.controller('MyCtrl', ['$scope',function($scope){
$scope.loadData = function(){
console.log("加载数据中...");
}
}]); app.controller('MyCtrl2', ['$scope',function($scope){
$scope.loadData2 = function(){
console.log("加载数据中...2222");
}
}]); app.directive("loader",function(){
return{
restrict: "AE",
link:function(scope,element,attrs){ /*link函数有4个参数,最后一个父控制器,此处用了3个*/
element.bind('mouseenter',function(){
// scope.$apply("loadData()");
// 注意这里的坑,howtoload会被转换成小写的howtoload
scope.$apply(attrs.howtoload);
});
}
}
});

angular directive指令的复用的更多相关文章

  1. angular directive指令内的参数

    angular.module('myApp', []) .directive('myDirective', function() { return { restrict: String, priori ...

  2. angular directive指令相互独立

    想要让指令的使用相互间不干扰,如下:

  3. Angular之指令Directive系列

    项目筹备近期开启Angular学习,指令比较难理解所以记录备案,推荐Angualr实战学习视频大漠穷秋 Angular实战 一.指令directive概述 指令可以对元素绑定事件监听或者改变DOM结构 ...

  4. Angular自定义指令(directive)

    angular自定义指令,意我们可以通过angula自己定义指令,来实现我们的特殊要求,为所欲为,一支穿云箭,千军万马来相见 多少年的老规矩了,先看代码: <!DOCTYPE html> ...

  5. angular 自定义指令详解 Directive

    在angular中,Directive,自定义指令的学习,可以更好的理解angular指令的原理,当angular的指令不能满足你的需求的时候,嘿嘿,你就可以来看看这篇文章,自定义自己的指令,可以满足 ...

  6. angular上传获取图片的directive指令

    在AngularJS中,操作DOM一般在指令中完成,那么指令是如何实现的呢?指令的作用是把我们自定义的语义化标签替换成浏览器能够认识的HTML标签 一般的事件监听是在对静态的dom绑定事件,而如果在指 ...

  7. 学习AngularJs:Directive指令用法(完整版)

    这篇文章主要学习AngularJs:Directive指令用法,内容很全面,感兴趣的小伙伴们可以参考一下   本教程使用AngularJs版本:1.5.3 AngularJs GitHub: http ...

  8. AngularJS中Directive指令系列 - 基本用法

    参考: https://docs.angularjs.org/api/ng/service/$compile http://www.zouyesheng.com/angular.html Direct ...

  9. directive 指令一

    什么是Directive Directive将一段html,js封装在一起,形成一个可以复用的独立个体,具有特定的功能.angularjs中的指令通常是比较小的组件,它相当于是给我们提供了一些公共的自 ...

随机推荐

  1. Codeforces Round #356 (Div. 2)B. Bear and Finding Criminals(水题)

    B. Bear and Finding Criminals time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  2. DataGridView操作

    C# DataGridView控件动态添加新行 DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如 ...

  3. ylbtech-dbs:ylbtech-3,BarCode(条码资源系统)

    ylbtech-dbs:ylbtech-3,BarCode(条码资源系统) -- =============================================-- 条码资源系统-- YU ...

  4. ylbtech-Recode(记录)-数据库设计

    ylbtech-dbs:ylbtech-Recode(记录)-数据库设计 -- =============================================-- DatabaseName ...

  5. Gerrit清单库配置(转载)

    From:http://fatalove.iteye.com/blog/1340334 gerrit清单库是用来配合repo使用的.清单库中列出了gerrit服务器上的其他版本库. 客户端通过repo ...

  6. sed 使用 删除匹配行

    “p” command prints the buffer (remember to use -n option with “p”) “d” command is just opposite, its ...

  7. ServiceBroker创建流程

    首先为这个数据库开启Service Broker ALTER DATABASE [T_EIP_UnityStore] SET ENABLE_BROKER 创建MessageType CREATE ME ...

  8. Ext vtype

    //form验证中vtype的默认支持类型1.alpha //只能输入字母,无法输入其他(如数字,特殊符号等)2.alphanum//只能输入字母和数字,无法输入其他3.email//email验证, ...

  9. Hibernate 只获取外键id,不获取内容

    Hibernate,jpa注解映射中 A多对一B A的表中有B的外键. 如果想只获取A表中的B的外键而不想发送查询B的sql语句. 那么: @ManyToOne(fetch=FetchType.LAZ ...

  10. cwRsync 配置文件详解

    GLOBAL PARAMETERS(全局参数) The first parameters in the file (before a [module] header) are the global p ...