如何编写ngBindCompile指令?

时间:2013-06-13 07:58:26

标签: javascript angularjs

与ngBindTemplate类似的指令如何,而不是采用作为模板的字符串,采用包含模板的变量,即:


Existing:

    ng-bind-template="{template}"

To write:

     ng-bind-compile="var"

     where var="{template}"

提前致谢!

1 个答案:

答案 0 :(得分:0)

以下是如何在指令的父作用域的上下文中使用$compile来完成:

app.directive('ngBindCompile',function($compile){
    return {
        scope:{
            template: '=ngBindCompile'
        },
        link: function(scope,element,attrs){
            var html = '<div>' + scope.template + '</div>';
            var compiled = $compile(html)(scope.$parent);
            element.replaceWith(compiled);
        }
    }
});

Here is a fiddle