简体   繁体   English

AngularJS有没有办法在boostrap之后动态注册一个指令

[英]AngularJS Is there a way to dynamically register a directive after boostrap

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 这是一个部分工作的例子: 小提琴

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, ie use restrict:'E' 指令注册正常,但限制是一个问题,你有元素限制,你需要明确,即使用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) 在观察属性时,不要使用scope.$watch代替使用attrs.$observe ,在你的情况下是attrs.$observe('dynamicDirective', fn) ,或者使用范围与scope:{dynamicDirective:'@'}结合scope:{dynamicDirective:'@'} anc使用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. 为了在使用attr或使用@绑定范围时使用指令时传递值并使其可观察使用插值,如果使用2方式=你不需要它。

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 Plnkr

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

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