AngularJS Is there a way to dynamically register a directive after boostrap

Rob

I want to dynamically load and compile a directive some time after the application is bootstrap.

Like when I press a button I want to run code similar to this:

app.directive('dynamicDirective',
    ['$compile', '$timeout', 'searchBuilderFactory',
    function ($compile, $timeout, searchBuilder) {
        return function (scope, element, attrs) {
            scope.$watch(attrs.truBindNgHtml, function (newValue, oldValue) {
                if (newValue === oldValue) return;
                var directiveName = 'advanced' + newValue;

                app.directive(directiveName, function() {
                    return {
                      template: 'Name: {{customer.name}} Address: {{customer.address}}'
                    };
                }); 

                var html = '<' + directiveName + '>' + '</' + directiveName + '>';

                element.html(html);
                $compile(element.contents())(scope);
            });
        };
}]);  

Here is a partially working example: Fiddle

PSL

You got it almost right, I am not exactly sure about the part in the controller which does, however you have some issues.

  • Directive is registered fine, but restriction is a problem, you have element restriction which you need to be explicit about, i.e use restrict:'E'

  • When watching attributes, do not use scope.$watch instead use attrs.$observe, in your case attrs.$observe('dynamicDirective', fn), or use scope binding with scope:{dynamicDirective:'@'} anc use scope.$watch('dynamicDirective', fn)

  • Inorder to pass values and have it observable use interpolation when consuming the directive while using attr or scope binding with @, if using 2 way = you do not need that.

Try:-

function ($compile) {

    return function (scope, element, attrs) {
        attrs.$observe('dynamicDirective', function (v, ov) {
            if (!v) return; //If no value do nothing
            var directiveName = 'advanced' + v;

            app.directive(directiveName, function () {
                return {
                    template: '<p>Blah</p>',
                    restrict: 'E'
                };
            });

            var newEl = '<' + directiveName + '>' + '</' + directiveName + '>';
            element.html(newEl); //or element.append(); if you want to add upon
            $compile(element.contents())(scope);

        });
    }
}]);

Plnkr

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically register directive at runtime

From Dev

AngularJS dynamically adding Directive

From Dev

AngularJS - adding directive dynamically to an element

From Dev

One way binding on AngularJS with a directive

From Dev

AngularJS : Best way to use directive

From Dev

One way binding on AngularJS with a directive

From Dev

Is there a way to detect directive destruction in AngularJS?

From Dev

AngularJS : Best way to use directive

From Dev

How to re-register a dynamically loaded angular templates' directive

From Dev

How to re-register a dynamically loaded angular templates' directive

From Dev

angularjs directive set template url dynamically

From Dev

Directive inside the dynamically created elements not working with angularjs

From Dev

How to dynamically add a directive to an input element in AngularJS

From Dev

Accessing parent controller in dynamically generated AngularJS directive

From Dev

Angularjs add bootstrap classes dynamically using directive

From Dev

AngularJS - dynamically created directive's "require" not working

From Dev

Dynamically setting attributes on an AngularJS Directive element

From Dev

Angularjs dynamically adding and removing elements using directive

From Dev

Directive inside the dynamically created elements not working with angularjs

From Dev

AngularJS - dynamically created directive's "require" not working

From Dev

AngularJs create directive dynamically from a list

From Dev

AngularJS Updating attribute in directive linking function dynamically

From Dev

Angularjs dynamically adding and removing elements using directive

From Dev

AngularJS: dynamically appending child div from directive

From Dev

AngularJS Directive: Pass dynamically created object as attribute for nested directive

From Dev

AngularJS Directive: Pass dynamically created object as attribute for nested directive

From Dev

Is there a way in angularjs to dynamically set ngModel?

From Dev

What is the idiomatic way to test an AngularJS directive?

From Dev

How to create two-way directive for AngularJS?

Related Related

  1. 1

    Dynamically register directive at runtime

  2. 2

    AngularJS dynamically adding Directive

  3. 3

    AngularJS - adding directive dynamically to an element

  4. 4

    One way binding on AngularJS with a directive

  5. 5

    AngularJS : Best way to use directive

  6. 6

    One way binding on AngularJS with a directive

  7. 7

    Is there a way to detect directive destruction in AngularJS?

  8. 8

    AngularJS : Best way to use directive

  9. 9

    How to re-register a dynamically loaded angular templates' directive

  10. 10

    How to re-register a dynamically loaded angular templates' directive

  11. 11

    angularjs directive set template url dynamically

  12. 12

    Directive inside the dynamically created elements not working with angularjs

  13. 13

    How to dynamically add a directive to an input element in AngularJS

  14. 14

    Accessing parent controller in dynamically generated AngularJS directive

  15. 15

    Angularjs add bootstrap classes dynamically using directive

  16. 16

    AngularJS - dynamically created directive's "require" not working

  17. 17

    Dynamically setting attributes on an AngularJS Directive element

  18. 18

    Angularjs dynamically adding and removing elements using directive

  19. 19

    Directive inside the dynamically created elements not working with angularjs

  20. 20

    AngularJS - dynamically created directive's "require" not working

  21. 21

    AngularJs create directive dynamically from a list

  22. 22

    AngularJS Updating attribute in directive linking function dynamically

  23. 23

    Angularjs dynamically adding and removing elements using directive

  24. 24

    AngularJS: dynamically appending child div from directive

  25. 25

    AngularJS Directive: Pass dynamically created object as attribute for nested directive

  26. 26

    AngularJS Directive: Pass dynamically created object as attribute for nested directive

  27. 27

    Is there a way in angularjs to dynamically set ngModel?

  28. 28

    What is the idiomatic way to test an AngularJS directive?

  29. 29

    How to create two-way directive for AngularJS?

HotTag

Archive