最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS directive link function not working - Stack Overflow

programmeradmin2浏览0评论

/

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.

Unfortunately, I've only been able to get the directive working (which activates a jQuery popup) using the shorthand way defined in popup2. The longhand popup2 directive doesn't seem to work at all, and in particular the link function in my definition object is never called. What do I need to do to make this explicit link declaration to work?

http://jsfiddle/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.

Unfortunately, I've only been able to get the directive working (which activates a jQuery popup) using the shorthand way defined in popup2. The longhand popup2 directive doesn't seem to work at all, and in particular the link function in my definition object is never called. What do I need to do to make this explicit link declaration to work?

Share Improve this question asked Jul 23, 2012 at 23:43 CoderMD666CoderMD666 1,0071 gold badge9 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

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.

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');
        },
        pile: function() {
            console.log('pile');
        }
    };

});

fiddle: http://jsfiddle/ZWLzb/

发布评论

评论列表(0)

  1. 暂无评论