2017-02-14 45 views
0

我有这个指令编译:Angularjs:在角模板中使用linky和NG绑定,HTML一起

var app = angular.module('mobApp.services'); 
app.directive('compile', ['$compile', function ($compile) { 
    return function(scope, element, attrs) { 
     scope.$watch(
      function(scope) { 
       return scope.$eval(attrs.compile); 
      }, 
      function(value) {     
       element.html(value); 
       $compile(element.contents())(scope); 
      } 
     ); 
    }; 
}]) 

我在这里用它在我的模板:

<p compile="post.details | linky:'_blank'"></p> 

如果数据有任何链接其渲染很好,但它不像<b>&lt;b&gt;呈现文字。我只想<b>作为<b>而不使内部文字加粗。

如果我使用ng-html-bind所有工作正常,但链接不起作用。如果我使用linky链接工作正常,但渲染不起作用。

回答

1

你可以试试下面的改变吗?

var app = angular.module('mobApp.services'); 
app.directive('compile', ['$compile', function ($compile) { 
    return function(scope, element, attrs) { 
     scope.$watch(attrs.compile, function(html) { 
      element.html(html); 
      $compile(element.contents())(scope); 
     }); 
    }; 
}]) 
0

您可以编写自定义过滤器在第一次使用linky过滤器,把标签回在那里

module.filter('linkyWithHtml', function($filter) { 
 
    return function(value) { 
 
    var linked = $filter('linky')(value); 
 
    var replaced = linked.replace(/\&gt;/g, '>').replace(/\&lt;/g, '<'); 
 
    return replaced; 
 
    }; 
 
});