简体   繁体   English

使用AngularJS从JSON对象中过滤HTML

[英]Filter HTML out of JSON object with AngularJS

I'm currectly working on a Wordpress project. 我目前正在从事Wordpress项目。 But for a nice change I'm working with a JSON API, which only gives me the information I need. 但是,要进行一个不错的更改,我正在使用JSON API,它仅提供我所需的信息。

I'm only facing one problem at the moment. 我目前只面临一个问题。 The content part in my JSON contains HTML tags, which get printed on the screen, without actually using the HTML tags. 我的JSON中的内容部分包含HTML标记,这些标记被打印在屏幕上,而没有实际使用HTML标记。

The JSON output looks like this: JSON输出如下所示:

 [{
"ID": 11,
"title": "test",
"status": "publish",
"type": "page",
"author": {
    "ID": 1,
    "name": "admin",
    "slug": "admin",
    "URL": "",
    "avatar": "http:\/\/0.gravatar.com\/avatar\/401f2b3c91dee8969b193544c3d9a636&d=404&r=G",
    "meta": {
        "links": {
            "self": "http:\/\/geertvandelangenberg.nl\/wp\/wp-json.php\/users\/1",
            "archives": "http:\/\/geertvandelangenberg.nl\/wp\/wp-json.php\/users\/1\/posts"
        }
    }
},
"content": "<p>testtt<\/p>\n", 
}]

My HTML looks like this: 我的HTML看起来像这样:

<script src="http://geertvandelangenberg.nl/wp/wp-content/themes/twentythirteen/js/angular.min.js"></script>
<script>
    function PostsCtrlAjax($scope, $http) {
        $http({method: 'GET', url: 'http://geertvandelangenberg.nl/wp/wp-json.php/pages/'}).success(function(data) {
            $scope.posts = data; // response data 
        });
    }
</script>

    <div id="ng-app" ng-ap- ng-controller="PostsCtrlAjax">
        <div ng-repeat="post in posts">
            <h2>
                <a href='{{post.link}}'>{{post.title}}</a>
            </h2>
            <div class="time">
                {{post.date}} - {{post.author.name}}
            </div>
            <p>{{post.content}}</p>
        </div>
    </div>

Could anyone tell me how I can filter out the HTML tags in the JSON object? 谁能告诉我如何过滤掉JSON对象中的HTML标签?

Thanks in advance! 提前致谢!

Geert 盖尔特

EDIT 编辑

Thanks for you comments so far, could anyone please edit this jsbin, I can't seem to manage to get this to work, even with the AngularJS docs. 到目前为止,感谢您的评论,任何人都可以编辑此jsbin,即使使用AngularJS文档,我似乎也无法设法使其正常工作。 I'm still quite noobish with Angular, but if someone would help me, it'd be much appreciated :) 我对Angular仍然不太满意,但是如果有人可以帮助我,将不胜感激:)

jsbin.com/oRoqIJEC/1/edit jsbin.com/oRoqIJEC/1/edit

PS. PS。 output does not work on jsbin because of stupid Access-Control-Allow-Origin issues.. 由于愚蠢的Access-Control-Allow-Origin问题,输出在jsbin上不起作用。

ng-bind-html will render your HTML. ng-bind-html将呈现您的HTML。 Don't forget to inject ngSanitize into your controller though. 但是不要忘记将ngSanitize注入您的控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM