【问题标题】:how to parse custom html element using ng-bind-html in angularjs如何在angularjs中使用ng-bind-html解析自定义html元素
【发布时间】:2014-06-23 09:49:15
【问题描述】:

我需要使用 ng-bind-html 解析一个字符串。此字符串中包含一些自定义 html 标记。在使用 ng-bind-html 进行解析时,会出现 $sanitize:badparse 的错误。

请查看小提琴中的错误:http://jsfiddle.net/8zS4h/2/

在 stackoverflow 和 google 中阅读问题时,我发现如果我使用 $sce.trustAsHtml() 可能会有解决方案。

这解决了我的错误问题,但无法解析我的自定义 html 元素。 你可以在这里看到这个更新的小提琴:http://jsfiddle.net/8zS4h/3/

我正在努力寻找解决方案。

编辑: 只是为了添加更多信息,我从 rss 提要中获取这个字符串,所以有时它也可以有 "<http>""<http" 标签。这是失败的地方。 所以如果字符串像<http://www.<em>whitehouse</em>.gov/omb/circulars/a076/ 应该给出像http://www.<em>whitehouse</em>.gov/omb/circulars/a076/ 这样的输出

【问题讨论】:

    标签: angularjs ng-bind-html


    【解决方案1】:

    好吧,我成功地获得了像锚、下划线和粗体这样的工作标签,没有问题,看:

    Fiddle

    angular.module('ngBindHtmlExample', ['ngSanitize'])
      .controller('ngBindHtmlCtrl', ['$scope', function ngBindHtmlCtrl($scope) {
        $scope.myHTML =
            '<a href=\"http://google.com\">link</a> contests <u>of OMB</u> <b>Circular</b> A-76';
      }])
        .filter('to_trusted', ['$sce', function($sce){
            return function(text) {
                return $sce.trustAsHtml(text);
            }; 
    }]);
    

    好像解析正确

    【讨论】:

    • 这很好,但是如果我有像“”或“
    • 它工作正常,你只需要使用正确的语法。例如。 很好,但是这个 '' 和结束元素
    • 这是个问题,我们从 rss 提要获取这个字符串,在将其插入数据库之前,我们将截断字符串的前 50 个字符。现在它可以有一个结束 '
    猜你喜欢
    • 2013-06-18
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多