如何编写类似于ngBindTemplate的指令,而不是采用字符串作为模板,而是采用包含模板的变量?即:
现有:
ng-bind-template="{template}"
撰写:
ng-bind-compile="var"
其中var="{template}"
提前感谢!
这是在指令的父范围的上下文中使用$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);
}
}
});