调用由控制器指令的范围内定义的函数 [英] Calling a function defined in a directive's scope from a controller

查看:150
本文介绍了调用由控制器指令的范围内定义的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改:我创建了一个Plnkr
http://plnkr.co/edit/yuvtrPs3csAQZW7OF06O

EDIT: I created a Plnkr http://plnkr.co/edit/yuvtrPs3csAQZW7OF06O

我不知道这有什么关系,但我注意到控制台的日志显示:

I'm not sure if this has anything to do, but I noticed the console's log shows:

事件跟踪Multipane显示preVIEW工具栏未定义未定义

Event tracked Multipane Show Preview Toolbar undefined undefined

premise:我创建一个新的问题在下面的链接可能被遗忘的问题:


Premise: I'm creating a new question following the "probably forgotten" question at the link:

<一个href=\"http://stackoverflow.com/questions/29197586/angularjs-how-do-i-call-a-function-defined-in-a-directives-scope-from-a-contro\">AngularJS:我如何调用从控制器指令的范围内定义的函数?

自从我得到了一个答案,我的情况下,告诉他新的问题,他希望在这里完成他的回答。
另外,我知道有类似的问题,但我找不到适合我的情况的解决方案。

Since I got one answer there, I informed him of the new question in case he wants to complete his answer here. Also, I know there are similar questions, but I can't find a solution suitable for my case.

我需要调用属于我的角度应用中使用的NG-指令的范围$的函数。
在下面的教程:

I need to call a function which belongs to the $scope of a ng-directive used in my Angular application. Following the tutorial at:

http://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/

我设置以下code为例。

I set up the following code as example.

假设该指令是这样定义的:(具体指令 - 在这里简单 - 就是由插件定义的:www.ixtendo.com/polyglot-language-switcher-2/#angularjs)

Let's say the directive is defined like this: (concretely the directive -- simplified here -- is the one defined by the plugin: www.ixtendo.com/polyglot-language-switcher-2/#angularjs )

注意:有不可以亲子的指令和控制,我想使访问指令的控制器功能的关系。这就是为什么我使用 $ rootScope

Note: there is not parent-child relation between the directive and the controller which I want to make access the directive's controller's function. This is why I use $rootScope

.directive('my-directive', ['$document', '$timeout', '$rootScope', function ($document, $timeout, $rootScope) {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            // ....
        },
        controller: ['$scope', '$rootScope', function ($scope, $rootScope) {

            var myFunction= function () {
                // do something...
            };

            $scope.$on('openLanguageSelectionPopup', function (){
                console.log("event triggered"); // NOT triggered!
                myFunction();  
            });
        }
    };
}]);

我需要调用的 myFunction的从我的控制器(名为 mainController ),这是我的指令被放置在视图控制器。我把它定义为:

I need to call myFunction from my controller (called mainController) which is the controller of the view where my directive is placed. I defined it as:

.controller('mainController',
function ($scope, $rootScope){
    $rootScope.$emit('openLanguageSelectionPopup'); 
    // this is event should be received by any $rootScope.$on(...) in ANY controller
})

我修改指令的控制器本身,但没有成功。

I modified the directive's controller itself but unsuccessfully

在事实上的情况下( openLanguageSelectionPopup )不会触发(不打印的console.log)。
我也相信,无论在$和$ EMIT都在某个点达到了(我试过的console.log(...))。

In fact the event (openLanguageSelectionPopup) is not triggered (console.log is not printed). I am also sure that both the $on and the $emit are reached at some point (I tried with console.log(...) ).

我明白这个问题可能在指令的范围定义提出如下面的答案的解决方案:

I understand the problem may be in directive's scope definition as proposed as solution in the following answer:

<一个href=\"http://stackoverflow.com/questions/26018812/calling-a-method-in-a-controller-defined-in-a-directive-from-a-javascript-functi\">Calling从javascript函数在指令定义在控制器的方法

Calling a method in a controller defined in a directive from a javascript function

但是,如果这是问题,我无法理解接受的答案可以给我的具体情况。

However, if this is the problem, I can't understand what the accepted answer can bring to my specific case.

另外,我不明白的地方我应该把$ rootScope :该指令的控制器功能或指令的定义函数(在我的例子中我加入这两个)

Also, I don't understand where I should put $rootScope: in the directive's controller function or in the directive's definition function (in my example I added it in both).

推荐答案

您发出,并在控制器广播你的东西。
但该指令并不存在。

You emit and broadcast your stuff in the controller. But the directive doesn't exist.

至少我认为这正在发生。
因为如果我在超时换你的广播,他们接受它。

Atleast i think that's happening. Because if i wrap your broadcast in a timeout they receive it.

此外,您的plunkr缺少出头

Also your plunkr is missing somethings

您忘了脚本添加到的index.html
而你打电话给你的指令,在这样的HTML,但它应该是

You forgot to add the script to the index.html And you call your directive in the html like this but it should be

这篇关于调用由控制器指令的范围内定义的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆