How to make AngularJS compile the code generated by directive?

前端 未结 3 948
春和景丽
春和景丽 2020-12-30 12:25

Please help me on, How can we make AngularJS compile the code generated by directive ?

You can even find the same code here, http://jsbin.com/obuqip/4/edit

<

3条回答
  •  既然无缘
    2020-12-30 12:48

    Code: http://jsbin.com/obuqip/9/edit

    components.directive('helloWorld', function() {
        var directiveObj =  {
            compile:function(element, attrs) {
                var strTemplate, strUserT = attrs.myUsername || "";
                console.log(strUserT);
                if(strUserT) {
                    strTemplate = "
    Hello " + "{{" + strUserT +"}}
    " ; } else { strTemplate = "
    Sorry, No user to greet!
    " ; } element.replaceWith(strTemplate); }, restrict: 'E' }; return directiveObj; });

    Explanation: The same code should be used in compile function rather than linking function. AngularJS does compile the generated content of compile function.

提交回复
热议问题