如何编写ngBindCompile指令?

问题描述 投票:1回答:1

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

现有:

ng-bind-template="{template}"

撰写:

 ng-bind-compile="var"

其中var="{template}"

提前感谢!

javascript html angularjs angularjs-directive angularjs-compile
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.