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的方法的更多相关文章

  1. 方法调用---springMVC中调用controller的方法

    我们有一个路由StudentController,里面有一个方法count().如果要在另外一个GradeController中调用count()方法有2种方式: 因为StudentControlle ...

  2. cocos2d-x 3.0 在C++中调用lua函数

    代码用的是<cocos2d-x 3.0 在lua中调用自定义类>中的代码. 在上篇的基础上进行扩充. 写lua函数 local function process_packet(user_d ...

  3. JavaScript文件中调用AngularJS内部方法或改变$scope变量

    需要在其他JavaScript文件中调用AngularJS内部方法或改变$scope变量,同时还要保持双向数据绑定: 首先获取AngularJS application: 方法一:通过controll ...

  4. PySpark 的背后原理--在Driver端,通过Py4j实现在Python中调用Java的方法.pyspark.executor 端一个Executor上同时运行多少个Task,就会有多少个对应的pyspark.worker进程。

    PySpark 的背后原理 Spark主要是由Scala语言开发,为了方便和其他系统集成而不引入scala相关依赖,部分实现使用Java语言开发,例如External Shuffle Service等 ...

  5. python学习-65 继承2-子类中调用父类的方法

    子类中调用父类的方法 1.子类继承了父类的方法,然后想进行修改,那么就需要在子类中调用父类的方法. 2.方法一:父类名 class School: Country = 'china' def __in ...

  6. React Hooks中父组件中调用子组件方法

    React Hooks中父组件中调用子组件方法 使用到的hooks-- useImperativeHandle,useRef /* child子组件 */ // https://reactjs.org ...

  7. 如何在adapter 中调用activity的方法

    如何在adapter 中调用activity的方法 2015-08-07 17:06匿名 | 浏览 808 次  iWorkjavaAndroid public class HistoryData e ...

  8. 在ASP中调用DLL的方法

    .net的dll已经不是严格意义上的动态连接库了,而是一个类或者类库.它是不能直接在ASP.VB等其它的应用环境中使用的.   我们可以通过COM包装器(COM callable wrapper (C ...

  9. SSM(Spring)中,在工具类中调用服务层的方法

    因为平时在调用service层时都是在controller中,有配置扫描注入,spring会根据配置自动注入所依赖的服务层. 但因我们写的工具类不属于controller层,所以当所写接口需要调用服务 ...

随机推荐

  1. HashMap源码-描述部分

    /** * Hash table based implementation of the <tt>Map</tt> interface. This * implementati ...

  2. squid.con 配置文件详解

    博客转载:http://www.articleswriting.net/article/6477447043/;jsessionid=42C9702B475ECF99EB861214186390E8 ...

  3. Shell--Bash shell的操作环境

    一.路径与命令查找顺序 1.以相对/绝对路径执行命令,例如“/bin/ls”或“./ls”; 2.由alias找到该命令来执行 3.由bash内置的(builtin)命令来执行 4.通过$PATH这个 ...

  4. Hibernate3和4版本的不同

    hibernate4的改动较大只有spring3.1以上版本能够支持,Spring3.1取消了HibernateTemplate,因为Hibernate4的事务管理已经很好了,不用Spring再扩展了 ...

  5. tensorflow TensorArray 代码例子

    import tensorflow as tf import numpy as np B=3 D=4 T=5 tf.reset_default_graph() xs=tf.placeholder(sh ...

  6. [Functional Programming] Transition State based on Existing State using the State ADT (liftState, composeK)

    While sometimes outside input can have influence on how a given stateful transaction transitions, th ...

  7. [GraphQL] Query a GraphQL API with graphql-request

    To query a GraphQL API, all you need to do is send an HTTP request that includes the query operation ...

  8. 倍福TwinCAT(贝福Beckhoff)基础教程6.1 TwinCAT如何与高级语言通讯

    因为使用TwinCAT的人用途不同,重视点就不同.如果用来代替传统PLC+HMI做项目的,很少会需要用到跟高级语言通讯,但是如果是用来做运动控制平台如做机器人运动控制器的,就肯定会用到.不管是否用得上 ...

  9. Java中的回调函数学习-深入浅出

    Java中的回调函数一般来说分为下面几步: 声明回调函数的统一接口interface A.包括方法callback(); 在调用类caller内将该接口设置为私有成员private A XXX; 在c ...

  10. IOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 转载

    http://blog.csdn.net/he_jiabin/article/details/48677911 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为 ...