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

Posted by user2316667 on Stack Overflow See other posts from Stack Overflow or by user2316667
Published on 2014-08-19T15:19:57Z Indexed on 2014/08/19 16:20 UTC
Read the original article Hit count: 182

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/

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. This way, in the script tag, all one needs to do is implement the 'remove'.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about angularjs