AngularJS:如何调用指令中定义的函数'控制器的作用域
AngularJS: How do I call a function defined in a directive's scope from a controller?
我需要调用一个函数,该函数属于Angular应用程序中使用的ng指令的$scope。
假设指令定义如下:
.directive('my-directive', ['$document', '$timeout', function ($document, $timeout) {
return {
restrict: 'E',
replace: true,
scope: {
// ....
},
controller: ['$scope', function ($scope) {
$scope.myFunction= function (mouseEnter) {
// ...
};
}
};
}]);
我需要从我的控制器调用myFunction(我们称之为my controller),它是放置指令的视图的控制器。
有可能做到吗?(最终修改指令)
编辑:已经回答的问题(建议编辑)与我的问题相似,因为我不清楚,或者它显然没有解决我提出的具体问题。
EDIT 2:从Dan M.的回答开始(没有考虑mouseenter/mouseleve。只是试图让两个控制器相互通信),我通过$rootScope向指令的控制器广播了我的事件(因为两个控制器之间没有父子关系),方法是:
console.log("let's broadcast the event.."); // this is printed
$rootScope.$broadcast('callDirectiveControllersFunction'); // I even tried with $scope in place of $rootScope and $emit in place of $broadcast
并通过以下方式接收(在指令的控制器内):
var myFunction = function(){
// ...
}
$scope.$on('callDirectiveControllersFunction', function (){
console.log("event received"); // this is not printed
callMyFunction();
});
// I even tried using $rootScope in place of $scope
但是,在无任何情况(请参阅代码中的注释)中,会收到
您可以在链接块内调用控制器函数。您还可以在指令中$emit一个事件,并在控制器中侦听它(可能有这样的用例)。
你似乎想在mouseenter
上调用它。您可以通过绑定到指令链接中的mouseenter
事件来执行此操作。问题是您需要$应用这些更改。看看下面的一段代码,它包含了所有3个示例:http://jsbin.com/cuvugu/8/.(也粘贴在下面)
提示:您可能需要注意如何命名指令。要将指令用作my-directive
,需要将其命名为myDirective
。
var app = angular.module('App', []);
app.directive('myDirective', function () {
function directiveLink(scope){
scope.$emit('customEvent');
}
return {
restrict: 'EA',
scope: {},
link: directiveLink,
controller: function ($scope) {
$scope.bar = 'bar';
$scope.myFunction = function () {
$scope.bar = 'foobar1';
};
$scope.$on('customEvent', function (){
$scope.myFunction();
});
},
template: "Foo {{bar}}"
};
});
app.directive('anotherDirective', function () {
function directiveLink(scope){
scope.myFunction();
}
return {
restrict: 'EA',
scope: {},
link: directiveLink,
controller: function ($scope) {
$scope.bar = 'bar';
$scope.myFunction = function () {
$scope.bar = 'foobar2';
};
},
template: "Foo {{bar}}"
};
});
app.directive('mouseDirective', function () {
function directiveLink(scope, element){
element.bind('mouseenter', function(){
scope.$apply(function(){
scope.myFunction();
});
});
element.bind('mouseleave', function(){
scope.$apply(function(){
scope.myOtherFunction();
});
});
}
return {
restrict: 'EA',
link: directiveLink,
controller: function ($scope) {
$scope.bar = 'no';
$scope.myFunction = function () {
$scope.bar = 'yes';
};
$scope.myOtherFunction = function () {
$scope.bar = 'no';
};
},
template: "Mouse Enter: {{bar}}"
};
});
我还在JS Bin链接中包含了一个带有不同控制器的示例。这并没有真正改变任何事情,但它似乎是你问题的重要组成部分。这是代码块:
var app = angular.module('App', []);
app.controller('myController', function($scope){
$scope.bar = 'foo';
$scope.myFunction = function(){
$scope.bar = 'foobar3';
};
});
app.directive('lastDirective', function () {
function directiveLink(scope){
scope.myFunction();
}
return {
restrict: 'EA',
scope: {},
link: directiveLink,
controller: 'myController',
template: "Foo {{bar}}"
};
});
相关文章:
- 用嵌套函数和默认函数定义函数
- 使用Extjs进行函数定义
- JS中奇怪的函数定义语法
- 重写javascript函数定义
- 使用角度图时,我可以用函数定义数据点颜色吗
- modal.js中的匿名函数定义
- 为从AJAX调用中utlizes DATA参数的函数定义回调
- Emacs:在 etag 中查找函数定义
- Javascript:对象函数定义
- 为什么要在函数定义之外引用静态变量(函数属性)
- Ember 对象函数定义中的计算属性.js
- javascript/jQuery中函数定义的区别
- CoffeeScript 承诺与函数定义链接
- 如何在使用 eval() 评估 JavaScript 时将回调函数定义作为参数传递
- Javascript - 为什么下面的函数定义错误
- 使用函数声明时的自定义函数(也称为惰性函数定义)
- JavaScript 函数定义不正确
- jQuery off on 时是用匿名函数定义的
- 停止 IntelliJ 11 在启用 JSLint 时向每个嵌套函数定义添加“use strict”
- 为什么不是't我为这个函数定义的javascript函数