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层,所以当所写接口需要调用服务 ...
随机推荐
- Linux守护进程的启动方法(转)
作者: 阮一峰 日期: 2016年2月28日 "守护进程"(daemon)就是一直在后台运行的进程(daemon). 本文介绍如何将一个 Web 应用,启动为守护进程. 一.问题的 ...
- 用字符串连接SQL语句并用EXEC执行时,出现名称 '‘不是有效的标识符
原文:用字符串连接SQL语句并用EXEC执行时,出现名称 ''不是有效的标识符 用字符串连接SQL语句并用EXEC执行时,出现名称 '这里是字符串连接的一条SQL语句‘不是有效的标识符 才发现,在写e ...
- win7/win10+vs2015+pcl1.8.0详细配置方案;
参考网友的资料整理为更详细的解决方案 一.下载相关文件 1.下载PCL-1.8.0-AllInOne-msvc2015-win64.exe.属性表和PDB和测试pcd文件rabbit.pcd,其中,属 ...
- hive 导入csv文件
创建hive表: create table table_name( id string, name string, age string ) row format serde 'org.apache. ...
- Yum编译安装Error Downloading Packages报错
1:执行yum clean all 清除缓存目录下的软件包及旧的headers: 2:接着执行 yum list重新列出所有已经安装和可以安装的软件包: 3:重新执行上述命令,发现yum编译成功: 注 ...
- hdu4671 思维构造
pid=4671">http://acm.hdu.edu.cn/showproblem.php? pid=4671 Problem Description Makomuno has N ...
- [React] Use react-rewards to add microinteractions to React app to reward users for some actions
It's important that our users enjoy using our application or website. One way we can make it happen ...
- 构建Spring Boot程序有用的文章
构建Spring Boot程序有用的文章: http://www.jb51.net/article/111546.htm
- Android模块编译过程中的错误no rules to make target
今天花了不少时间在纠正一个编译错误: make: *** No rule to make target `out/target/common/obj/JAVA_LIBRARIES/sqlite-jdb ...
- ionic准备之angular基础——dom操作相关(6)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...