角度链接没有消毒(过滤器)

时间:2013-06-28 21:09:48

标签: angularjs

新角度。我正在使用linky过滤器为一些已经清理过的文本添加href链接,其中包含换行符。

http://docs-angularjs-org-dev.appspot.com/api/ngSanitize.filter:linky angularjs newline filter with no other html

<section class="description"
       ng-bind-html-unsafe="piano.description | noHTML | newlines | linky ">
</section>

然而,linky会自动清理html,删除我精心添加的换行符。似乎没有一种可接受的方法可以做到这一点,哪种方法效果很好?制作过滤器以取消我的换行符?自定义linky以添加无过滤选项?

3 个答案:

答案 0 :(得分:1)

我今天遇到了同样的问题 - 最终通过从这里用parseUrlFilter替换Angular的内部linky-filter来解决它:https://stackoverflow.com/a/14693341/2199358

答案 1 :(得分:0)

您在标题中回答了自己的问题。在没有消毒的情况下使用角度的链接。我刚刚进入Angular的源码,复制粘贴角度的链接函数而没有进行清理,并将其重命名。

完整代码:

Angularjs directive to replace text

答案 2 :(得分:0)

我在类似的情况下最终创建了以下指令。它做了两件事:

  1. 无HTML清理
  2. 标记在当前范围内编译。
  3. 用法:

    <tr ng-repeat="row in newItemHTMLs" replace-with-html="row"></tr>
    

    来源:

    myApp.directive('replaceWithHtml', ['$parse', '$compile', function($parse, $compile) {
        return {
          restrict: 'A',
          compile: function(tElement, tAttrs) {
            var ngBindHtmlGetter, ngBindHtmlWatch;
    
            ngBindHtmlGetter = $parse(tAttrs.replaceWithHtml);
            ngBindHtmlWatch = $parse(tAttrs.replaceWithHtml, function(value) {
              return (value || '').toString();
            });
            $compile.$$addBindingClass(tElement);
    
            return function(scope, element, attr) {
    
              $compile.$$addBindingInfo(element, attr.replaceWithHtml);
    
              return scope.$watch(ngBindHtmlWatch, function() {
                return element.replaceWith($compile(ngBindHtmlGetter(scope))(scope) || '');
              });
    
            };
    
          }
        };
      }
    ]);