AngularJS:呼叫从一个指令控制器功能,而不孤立范围 [英] AngularJS : Call a Controller Function from a directive without isolated scope

查看:115
本文介绍了AngularJS:呼叫从一个指令控制器功能,而不孤立范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到一个方法来调用父范围函数从指令中不使用分离的范围。我知道,如果我用孤立的范围,我可以只使用与&在分离来访问父范围的功能,但是使用分离范围时,没有必要有后果。请看下面的HTML:

I cannot seem to find a way to call a function on the parent scope from within a directive without using isolated scope. I know that if I use isolated scope I can just use "&" in the isolated to access the function on the parent scope, but using isolated scope when it isn't necessary has consequences. Consider the following html:

<button ng-hide="hideButton()" confirm="Are you sure?" confirm-action="doIt()">Do It</button>

在这个简单的例子,我想说明一个JavaScript确认对话框,只调用的doIt(),如果他们点击确定,在确认对话框。这是简单的用一个孤立的范围。该指令是这样的:

In simple this example, I want to show a javascript confirm dialog and only call doIt() if they click "ok" in the confirm dialog. This is simple using an isolated scope. The directive would look like this:

.directive('confirm', function () {
    return {
        restrict: 'A',
        scope: {
            confirm: '@',
            confirmAction: '&'
        },
        link: function (scope, element, attrs) {
            element.bind('click', function (e) {
                if (confirm(scope.confirm)) {
                    scope.confirmAction();
                }
            });
        }
    };
})

但问题是,因为我使用的隔离范围,NG-隐藏在上面的例子中不再执行对父母范围,而是在孤立的范围(因为使用一个孤立的范围在任何指令导致该元素使用隔离范围内)上的所有指令。 这里是NG隐藏不工作上面的例子中的的jsfiddle 。 (注意,在这个小提琴,按钮应,当你在输入框中键入是隐藏)。

But the problem is, because I'm using isolated scope, ng-hide in the example above no longer executes against the parent scope, but rather in the isolated scope (since using an isolated scope on any directive causes all directives on that element to use the isolated scope). Here is a jsFiddle of the above example where ng-hide is not working. (Note that in this fiddle, the button should hide when you type "yes" in the input box).

另一种方法是为不要用孤立的范围,这其实是我真正想在这里,因为没有必要对这个指令的范围被孤立。我唯一​​的问题是,我怎么调用父范围的方法,如果我不把它传递孤立的范围这里就是我不使用分离的范围和NG隐藏正在罚款的jsfiddle ,但是,当然,通话到confirmAction()不工作,我不知道如何使它发挥作用。

The alternative would be to NOT use an isolated scope, which actually is what I really want here since there is no need for this directive's scope to be isolated. The only problem I have is, how do I call a method on the parent scope if I don't pass it in on on isolated scope? Here is a jsfiddle where I am NOT using isolated scope and the ng-hide is working fine, but, of course, the call to confirmAction() doesn't work and I don't know how to make it work.

请注意,得到的答案我真正需要的是如何调用的外部范围功能,无需使用独立的范围。而且我不感兴趣,使这个确认对话框作品以另一种方式,因为这个问题的关键是要弄清楚如何拨打电话外范围,仍然能够有对父范围其他指令的工作。

Please note, the answer I am really looking for is how to call functions on the outer scope WITHOUT using an isolated scope. And I am not interested in making this confirm dialog work in another way, because the point of this question is to figure out how to make calls to the outer scope and still be able to have other directives work against the parent scope.

另外,我会感兴趣地听到,使用隔离范围的解决方案,如果其他指令仍将对父母范围工作,但我不认为这是可能的。

Alternatively, I would be interested to hear of solutions that use an isolated scope if other directives will still work against the parent scope, but I don't think this is possible.

推荐答案

由于该指令只调用一个函数(而不是试图设置一个属性的值),可以使用的$eval 代替$解析(与非分离范围内):

Since the directive is only calling a function (and not trying to set a value on a property), you can use $eval instead of $parse (with a non-isolate scope):

scope.$apply(function() { 
    scope.$eval(attrs.confirmAction); 
});

或更好的,只是简单地使用 $适用,这将$的eval()审视你们反对其范围参数:

or better, simply just use $apply, which will $eval()uate its argument against the scope:

scope.$apply(attrs.confirmAction); 

小提琴

这篇关于AngularJS:呼叫从一个指令控制器功能,而不孤立范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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