AngularJS指令链接功能不执行 [英] AngularJS directive link function not executing

查看:85
本文介绍了AngularJS指令链接功能不执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个包装因为这是我的工作模板的一部分jQuery的元素。

在这里例子中的链接方法: http://jsfiddle.net/Webnet/ugSsk/不会被触发。我无法得到它输出到控制台或警报。

有什么建议?

JS:

  angular.module(测试,[])。
    指令('的slideToggle',函数(){
        返回{
            适用范围:假的,
            更换:真实,
            模板:'<输入类型=复选框NAME =级=的slideToggle/>,
            链接:['范围','元素','ATTRS'功能(范围,元素,ATTRS){
                的console.log(元);
                警报('联');
            }],
            控制器:['$范围,$元素,$ ATTRS',函数($范围,$元,$ ATTRS){            }]
        }
    });

HTML

 < D​​IV NG-应用=测试>
    <有关= DIV切换开关主动关闭=无效>< / DIV>
< / DIV>


解决方案

所有的标注样式是等同的,可以在支撑注射角的任何地方使用。 - DI DOC

由于链接功能不支持注入,则不能使用内嵌注释(或任何其他DI标注样式)那里。

I'm writing a wrapper for a jQuery element that is part of a template I'm working with.

The link method in the example here: http://jsfiddle.net/Webnet/ugSsk/ is not triggered. I can't get it to output to the console or alert.

Any suggestions?

JS:

angular.module('test', []).
    directive('slideToggle', function () {
        return {
            scope: false,
            replace: true,
            template: '<input type="checkbox" name="" class="slideToggle"/>',
            link: ['scope', 'element', 'attrs', function (scope, element, attrs) {
                console.log(element);
                alert('linked');
            }],
            controller: ['$scope', '$element', '$attrs', function ($scope, $element, $attrs) {

            }]
        }
    });

HTML:

<div ng-app="test">
    <div slide-toggle on="Active" off="Inactive"></div>
</div>

解决方案

"all of the annotation styles are equivalent and can be used anywhere in Angular where injection is supported." -- DI doc

Since the link function doesn't support injection, you can't use the inline annotation (or any other DI annotation style) there.

这篇关于AngularJS指令链接功能不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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