简体   繁体   English

如何在角度指令的链接函数中定义角度指令?

[英]How to define an angular directive inside an angular directive's link function?

I want to create an angular directive inside of a link function, however; 我想在链接函数内部创建一个角度指令。 the directive created is not able to be compiled. 创建的指令无法编译。

See this JSFiddle: http://jsfiddle.net/v47uvsj5/5/ 看到这个JSFiddle: http : //jsfiddle.net/v47uvsj5/5/

Uncommenting this directive in the global space works as expected. 取消注释此指令在全局空间中的工作符合预期。

app.directive('test', function () {
    return {
        templateUrl: 'myform', // wraps script tag with id 'myform'
        restrict: 'E',
        require: "^mydir",
        replace: true,
        scope: {
        },
        link: function (scope, element, attrs, mydirCtrl) {
            scope.remove = function () {
                element.remove();
                mydirCtrl.remove();
            }
        }
    }
});

But the exact same code inside the link function fails. 但是链接函数中完全相同的代码将失败。

The reason I want to do this is because I want the user (who is going to be myself) to be able to provide only a script tag's id via an id attribute to my main directive which will in turn create a 'wrapper' directive with a 'remove' method. 我想要这样做的原因是因为我希望用户(将要成为我自己)能够通过id属性提供给我的main指令,从而仅提供一个脚本标签的id,这反过来将创建一个“ wrapper”指令,一种“删除”方法。 This way, in the script tag, all one needs to do is implement the 'remove'. 这样,在script标记中,所有要做的就是实现“删除”。

Check out this fiddle: 看看这个小提琴:

http://jsfiddle.net/v47uvsj5/8/ http://jsfiddle.net/v47uvsj5/8/

What I did in this fiddle was daisy chain your directives, which is the correct thing to do. 在这个小提琴中,我所做的就是将您的指令以菊花链方式连接,这是正确的做法。 When your app runs, it does a binding of each of your directive and builds your html as it's being compiled, then it links events to it. 当您的应用程序运行时,它将对您的每个指令进行绑定,并在编译时构建html,然后将事件链接到它。 Links and compilation happen after binding all directives to the DOM. 将所有指令绑定到DOM之后,就会进行链接和编译。

So <test></test> becomes <div></div> if you give it a template. 因此,如果给它提供模板,则<test></test>变为<div></div> If there is no template, nothing really builds your directive against the DOM, it just becomes empty, but you can still run a jquery script if you want. 如果没有模板,则没有任何东西可以真正针对DOM构建指令,它只是变成空的,但是您仍然可以根据需要运行jquery脚本。

Think of it like this, when your app loads up, it registers all the directives to be binded with the associated templates. 像这样想,当您的应用程序加载时,它会注册所有要与关联模板绑定的指令。 Afterwards, the app then "compiles" those directives by binding any kind of events to the newly established DOM. 然后,应用程序通过将任何类型的事件绑定到新建立的DOM来“编译”那些指令。 At this point, if no directives are registered during app load, the compile function ignores it. 此时,如果在应用程序加载期间未注册任何指令,则编译功能将忽略该指令。 In your case, you tried to bind the 'test' directive after the app load, and during the compilation. 在您的情况下,您尝试在应用程序加载之后和编译期间绑定“ test”指令。

This mechanism is analogous to how jquery's "on" works. 这种机制类似于jquery的“ on”工作方式。 When you do a "click" event on an already loaded DOM element, this fires up. 当您在已加载的DOM元素上执行“ click”事件时,将触发此事件。 But when you load html AFTER the DOM is finished, nothing works unless you use "on". 但是,当您在DOM完成后加载html时,除非您使用“ on”,否则任何操作均无效。

To be fair, the developers of angular did mention how there's a steep learning curve for handling directions, and will be revised to make it much easier in 2.0. 公平地说,angular的开发人员确实提到了如何处理陡峭的学习曲线,并将对其进行修订以使其在2.0中变得更加容易。 You can read about it in this blog here: Angular-2.0 您可以在以下博客中阅读有关它的信息: Angular-2.0

Anyways, 无论如何,

This is how your html should look like: 这是您的html外观:

<mydir><test></test></mydir>

and this is how you daisy chain: 这就是您的菊花链方式:

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

app.directive('mydir', function ($compile, $templateCache) {
    return {
        template: '',
        restrict: 'E',
        controller: function () {
           console.log("got it!");
        }
    }
}).directive('test', function () {
    return {
        templateUrl: 'myform',
        restrict: 'E',
        require: "^mydir",
        replace: true,
        scope: {
        },
        link: function (scope, element, attrs, mydirCtrl) {
            scope.remove = function () {
                element.remove();
                mydirCtrl.remove();
            }
        }
    }
});

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

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