2

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

http://docs-angularjs-org-dev.appspot.com/api/ngSanitize.filter:linky angularjs 换行过滤器,没有其他 html

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

但是,linky 会自动清理 html,删除我精心添加的换行符。似乎没有一种可接受的方式来做到这一点,什么会很好?制作一个过滤器来取消我的换行符?自定义链接以添加无过滤选项?

4

3 回答 3

1

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

于 2013-12-05T13:27:37.837 回答
0

你在标题中回答了你自己的问题。使用 Angular 的链接而不进行消毒。我刚刚进入 Angular 的源代码,复制粘贴了 Angular 的链接功能,没有进行清理,并将其重命名。

完整代码在这里:

Angularjs指令替换文本

于 2014-06-18T17:18:36.787 回答
0

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

  1. 没有 HTML 清理
  2. 标记在当前范围内编译。

用法:

<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) || '');
          });

        };

      }
    };
  }
]);
于 2016-06-07T23:21:04.750 回答