Angular.js中具有ng-bind-html的多个过滤器 [英] Multiple Filters with ng-bind-html In Angularjs

查看:63
本文介绍了Angular.js中具有ng-bind-html的多个过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在 ng-bind-html 中使用多个过滤器?

How can we use multiple filters with ng-bind-html?

我有两个过滤器.一种是自定义过滤器,我称其为 linkyHashes ,另一种是 ngsanitize linky过滤器.

I have two filters. One is custom filter, I called it linkyHashes and other is ngsanitize linky filters.

我通过以下方式使用此过滤器

I use this filter by

<div class="mts mbs lg-feed-content-txt" ng-bind-html="activity.post.text | linkyhashes | linky : '_blank'"></div>

但是我的自定义过滤器无效,只有链接过滤器有效

But my custom filter do not works, only linky filter works

这是我的自定义过滤器

LinkagoalWebApp.filter('linkyhashes' ,['$filter', '$sce',  function($filter, $sce){
    return function(text, target){
        if (!text) return text;
         var replacedText = $filter('linky')(text, target);
         var targetAttr = "";
         if (angular.isDefined(target)) {
         targetAttr = ' target="' + target + '"';
         }

        // replace #hashtags
        var replacePattern = /(?:#)([a-zA-Z|_\d]+)/ig;
        replacedText = text.replace(replacePattern, '<a class="hashtag" href="http://linkagoal.local/tagged/$1"' + targetAttr + '>#$1</a>');
        $sce.trustAsHtml(replacedText);
        return replacedText;
    }
}]);

推荐答案

我通过更改ng-bind-html

I solve that problem by changing ng-bind-html

ng-bind-html="activity.post.text | linkyhashes | linky : '_blank'"

ng-bind-html=" activity.post.text | linky : '_blank' | linkyhashes "

然后在过滤器中删除 $ filter('linky')(text,target);

这篇关于Angular.js中具有ng-bind-html的多个过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆