Angular1.0 在Directive中调用Controller的方法
Controller中定义了$scope.method = function(){}
Directive中需要引入$scope
http://stackoverflow.com/questions/23636727/how-to-call-controller-function-from-directive
JS:
angular.module('myApp', [])
.controller('MyController', function($scope){
$scope.showAlert = function(value){
alert('Called from directive: ' + value);
};
})
.directive('myDirective', function(){
return {
restrict: 'E',
scope: {
alert: '&'
},
controller: function($scope){
$scope.value = 'directive scope value';
},
template: '<button ng-click="alert({message: value})">Alert</button>'
}
});
HTML:
<body ng-app="myApp" ng-controller="MyController">
<my-directive alert="showAlert(message)"></my-directive>
</body>
My recommendation is to use $emit instead of calling a method of the controller directly in your directive.
Directives should be always independent components, if inside the directive there is a call to a method from a controller(outside the directive) this will create a dependency between my directive and the controller and of course this will force one not being able to exist without the other.
If I would have to apply a design principle to a directive it will be the S in SOLID, Single responsibility principle. Directives should be able to encapsulate and work independently.
On my controller the event is captured using $on like:
$scope.$on("ValueChanged", function(event, ars){
... //your event has been triggered.
});
或者
var directive= function ( $sessionStorage, $localStorage) {
function work(scope, element, attrs, formCtrl) {
var watchPromise = attrs.createClaimForm || null;
element.bind('keypress', function (event) {
//此处scope就是$scope
scope.addBlankRowData();
}
});
return {
require: "form",
restrict: "A",
link: work
}
};
Controller中this.method = function(){}是获取不到的, 必须$scope
Angular1.0 在Directive中调用Controller的方法的更多相关文章
- 方法调用---springMVC中调用controller的方法
我们有一个路由StudentController,里面有一个方法count().如果要在另外一个GradeController中调用count()方法有2种方式: 因为StudentControlle ...
- cocos2d-x 3.0 在C++中调用lua函数
代码用的是<cocos2d-x 3.0 在lua中调用自定义类>中的代码. 在上篇的基础上进行扩充. 写lua函数 local function process_packet(user_d ...
- JavaScript文件中调用AngularJS内部方法或改变$scope变量
需要在其他JavaScript文件中调用AngularJS内部方法或改变$scope变量,同时还要保持双向数据绑定: 首先获取AngularJS application: 方法一:通过controll ...
- PySpark 的背后原理--在Driver端,通过Py4j实现在Python中调用Java的方法.pyspark.executor 端一个Executor上同时运行多少个Task,就会有多少个对应的pyspark.worker进程。
PySpark 的背后原理 Spark主要是由Scala语言开发,为了方便和其他系统集成而不引入scala相关依赖,部分实现使用Java语言开发,例如External Shuffle Service等 ...
- python学习-65 继承2-子类中调用父类的方法
子类中调用父类的方法 1.子类继承了父类的方法,然后想进行修改,那么就需要在子类中调用父类的方法. 2.方法一:父类名 class School: Country = 'china' def __in ...
- React Hooks中父组件中调用子组件方法
React Hooks中父组件中调用子组件方法 使用到的hooks-- useImperativeHandle,useRef /* child子组件 */ // https://reactjs.org ...
- 如何在adapter 中调用activity的方法
如何在adapter 中调用activity的方法 2015-08-07 17:06匿名 | 浏览 808 次 iWorkjavaAndroid public class HistoryData e ...
- 在ASP中调用DLL的方法
.net的dll已经不是严格意义上的动态连接库了,而是一个类或者类库.它是不能直接在ASP.VB等其它的应用环境中使用的. 我们可以通过COM包装器(COM callable wrapper (C ...
- SSM(Spring)中,在工具类中调用服务层的方法
因为平时在调用service层时都是在controller中,有配置扫描注入,spring会根据配置自动注入所依赖的服务层. 但因我们写的工具类不属于controller层,所以当所写接口需要调用服务 ...
随机推荐
- 咏南下拉列表非数据敏感控件--TYNSearch
咏南下拉列表非数据敏感控件--TYNSearch 拥有下拉列表控件可以大大地加速软件系统的开发. 控件适用于DELPHI5及以上版本的安装和使用. 控件的使用方法: procedure Tflog.s ...
- Vue的常用指令v-if, v-for, v-show,v-else, v-bind, v-on
Vue.js的指令是以v-开头的,它们作用于HTML元素,指令提供了一些特殊的特性,将指令绑定在元素上时,指令会为绑定的目标元素添加一些特殊的行为,我们可以将指令看作特殊的HTML特性(attribu ...
- kubernetes 实用 api list
https://192.168.20.128:6443/api/v1/pods 原文来自https://segmentfault.com/a/1190000002937665 收集整理一些可能较常用的 ...
- C#使用StreamWriter类写入文件文件
除了使用FileStream类读写文本文件,.net还提供了StreamWriter类和StreamReader类专门处理文本文件.这两个类从底层封装了文件流,读写时不用重新编码,提供了更文件的读写方 ...
- Java获取运行环境信息
在做视频截取封面的时候用到了ffmpeg.我采用的是通过Java调用bat或sh脚本然后生成图片文件. 在线上使用的是Centos 7.所以程序中需要获取到当前运行环境的信息来选择调用bat命令还是s ...
- [转载]Lenovo E431 装Centos7无线驱动安装
FROM:http://huangyandong.blog.51cto.com/1396940/1613096 查看无线网卡型号 lspci |grep Network #为BCM43142网卡 ...
- Asp.net问题集锦
1.在Web应用开发中经常碰到这样的情况,Dropdownlist绑定的数据太多,用户要选择某一项必须从头找到尾,使用起来很不方便.最近我在工作中就碰到这种情况,公司内某个业务系统需要绑定几百条的厂家 ...
- 【Javascript 基础】对象
1 创建对象 Javascript 支持对象的概率.有多种方法可以用来创建对象. <!DOCTYPE html> <html lang="en"> < ...
- 如何用PS快速的批量制作连续号码数字编号图解
如何用PS快速的批量制作连续号码数字编号图解 大家好,今天太原博飞设计培训小编就告诉大家如用PS快速的制作连续数字编号,在工作中尤其是大型活动的有时候制作连续的号码牌,少还好,如果上百上千个,那就辛苦 ...
- Javascript中的回调函数和匿名函数的回调
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...