2013-06-13 54 views
1

如何將一個指令類似於ngBindTemplate,但不是採取一個字符串,它是模板,採用包含模板,即一個變量:如何編寫ngBindCompile指令?

 

Existing: 

    ng-bind-template="{template}" 

To write: 

    ng-bind-compile="var" 

    where var="{template}" 

提前感謝!

回答

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

+0

感謝您的幫助! – zcaudate