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层,所以当所写接口需要调用服务 ...
随机推荐
- centos iptables关于ping
配置iptables策略后,一般来说INPUT都是DROP然后配置需要通过的 当执行: iptables -P INPUT DROP 后,机器就不能被ping通了! 因为icmp没有添加到规则中! 于 ...
- LinuxPAServer19.0.tar.gz压缩包
LinuxPAServer19.0.tar.gz DELPHI XE10.2(TOKYO)开始可以编写LINUX控制台程序.在LINUX上面需要部署LinuxPAServer19.0.tar.gz,即 ...
- Git:fatal: The remote end hung up unexpectedly
一.配置公共密钥 https://help.github.com/articles/generating-ssh-keys/ 二.设置缓冲值(push文件较大时导致错误) \.git\config [ ...
- 去掉wget烦人的 “eta(英国中部时间)” 提示
gentoo 里的 wget ,从1.12版本开始,就一直有个不影响功能的小毛病:由于中文翻译的失误,进度提示的时候,会被拉成很多行.原因就是原来英文的ETA这3个字母,被翻译成了 “eta(英国中部 ...
- 管理 node 版本,选择 nvm 还是 n?
来源:http://taobaofed.org/blog/2015/11/17/nvm-or-n/ 引子 我本机安装着 nvm,而 node 本来一直运行在 0.x 的老版本上.后来为了跑 ES6,我 ...
- Java:集合类的区别详解
Java中集合类的区别 Array是数组,不在集合框架范畴之内,一旦选定了,它的容量大小就不能改变了,所以通常在编程中不选用数组来存放. 集合 : 集合对象:用于管理其他若干对象的对象 数组:长度不可 ...
- 浙大PAT考试1077~1080(2014上机复试题目)
题目地址:点击打开链接 还是太弱. . 英文太差.,, 预计要等待被虐了.. 1077 找最长的公共后缀,暴力就能够写: #include<iostream> #include<cs ...
- iptables 的学习资源
慕课网:https://www.imooc.com/video/7617 马哥linux视频:http://edu.51cto.com//center/course/lesson/index?id=9 ...
- 最简单简洁高效的Json数据解析
一.无图无真相 二.主要代码 1.导入jar包 拷贝fastjson.jar包到projectlibs包下 2.封装工具类JsonUtil.java package com.example.parse ...
- Java中的回调函数学习-深入浅出
Java中的回调函数一般来说分为下面几步: 声明回调函数的统一接口interface A.包括方法callback(); 在调用类caller内将该接口设置为私有成员private A XXX; 在c ...