2014-04-01 78 views
0

裏面我需要在NEWVALUE的HTML工作,但它似乎只是吐出逃脫charaters傳遞的HTML模板角指令

.directive('ngLookup', function() { 
    return { 
     restrict: 'A', 
     scope : { 
      text : '=' 
     }, 
     template: '{{newValue}}', 
     link: function (scope, elem, attrs) { 
      scope.newValue = scope.text.replace(/test/g,'<a href="javascript:;">test</a>');     
     } 
    } 
}) 

使用下面的答案我的解決辦法:

.directive('ngLookup', function ($compile) { 
    return { 
     restrict: 'EA', 
     scope : { 
      text : '=' 
     }, 
     link: function (scope, elem, attrs) { 
      var txt = '<span>'+scope.text.replace(/test/g,'<a href="javascript:;">test</a>')+"</span>"; 
      var newElement = $compile(txt)(scope); 
      elem.replaceWith(newElement); 
     } 
    } 
}) 

回答

0

你將不得不使用$compile -Service來自己編譯HTML。 做一些類似的事:

.directive('ngLookup', function ($compile) { 
    return { 
    restrict: 'A', 
    scope : { 
     text : '=' 
    }, 
    link: function (scope, elem, attrs) { 
     var newElement = $compile('<a href="javascript:;">test</a>')(scope); 
     elem.replaceWith(newElement); 
    } 
    } 
}) 
+0

這工作。非常感謝。 –