简体   繁体   English

AngularJS指令链接功能不起作用

[英]AngularJS directive link function not working

http://jsfiddle.net/kz26/kH9wg/ http://jsfiddle.net/kz26/kH9wg/

I'm playing around with directives in AngularJS and have trying both the shorthand directive style (returning only the link function) and the longhand style (returning all or part of a directive definition object. 我正在使用AngularJS中的指令,并尝试使用简写指令样式(仅返回链接函数)和longhand样式(返回指令定义对象的全部或部分内容)。

Unfortunately, I've only been able to get the directive working (which activates a jQuery popup) using the shorthand way defined in popup2 . 不幸的是,我只能使用popup2定义的速记方式使指令工作(激活jQuery弹出窗口)。 The longhand popup2 directive doesn't seem to work at all, and in particular the link function in my definition object is never called. popup2指令似乎根本不起作用,特别是我的定义对象中的link函数永远不会被调用。 What do I need to do to make this explicit link declaration to work? 我需要做些什么才能使这个显式链接声明起作用?

Both of your directives work with a small tweak to reuse the same module when creating the directives instead of overwriting the first one. 在创建指令时,两个指令都使用一个小的调整来重用相同的模块,而不是覆盖第一个指令。 See this fiddle . 看到这个小提琴

Instead of doing: 而不是做:

angular.module("app", []).directive('popover1'...

angular.module("app", []).directive('popover2'...

Do something like this: 做这样的事情:

var module = angular.module("app", []);

module.directive('popover1'...

module.directive('popover2'...

Edit: after looking at the docs I see you can do something similar to the original post as well like this: 编辑:看完文档后,我看到你可以做一些与原帖相似的内容,如下所示:

angular.module('app', []).directive('popover1'...

angular.module('app').directive('popover2'...

Omit the second parameter [] in subsequent calls after the first to angular.module to configure an existing module. 在第一个到angular.module之后的后续调用中省略第二个参数[]以配置现有模块。

And why the link function isnt called here?: 为什么链接功能不在这里调用?:

<div ng:app="app">
<div>
    <p test="">Hello!</p>
</div>

var module = angular.module("app", []);

module.directive('test', function() {
return {
        restrict: '',
        link: function () {
            console.log('linkfn');
        },
        compile: function() {
            console.log('compile');
        }
    };

});

fiddle: http://jsfiddle.net/ZWLzb/ 小提琴: http//jsfiddle.net/ZWLzb/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM