1

How can I extract certain tag, for example img when parsing JSON in AngularJS? I have a JSON file with following data:

jsonFeed({
"title": "My Blog",
"items": [
   {
      "title": "Title1",
      "description": "<p><a href=\"#">Paul<\/a> posted a photo:<\/p><img src=\"https://farm8.staticflickr.com/7365/26872407641_cfbb210ee7_m.jpg\"/>"
   },
   {
      "title": "Title2",
      "description": "<p><a href=\"#">Beth<\/a> posted a photo:<\/p><img src=\"https://farm8.staticflickr.com/7287/26333398074_cfbce73532_m.jpg\"/>"
   }
   ]
 });

I need to get that image in description for every post:

<article ng-repeat="post in posts">
  <h1>{{ post.title }}</h1>
  <img src=""/>
</article>

Is there way to do that in angularjs or is it better to use jQuery?

1
  • use substring and indexof method to get the img tag from description. use angular $sanitize to show html Commented May 11, 2016 at 17:38

5 Answers 5

1

You can use regex: ((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))

You can use $sce to bind the HTML: https://docs.angularjs.org/api/ngSanitize/service/$sanitize

var app = angular.module("app", []);

app.controller("MainCtrl", ["$scope", "$sce",
    function($scope, $sce) {
        $scope.jsonStr = {
            "title": "My Blog",
            "items": [{
                "title": "Title1",
                "description": "<p><a href=\"#\">Paul<\/a> posted a photo:<\/p><img src=\"https://farm8.staticflickr.com/7365/26872407641_cfbb210ee7_m.jpg\"/>"
            }, {
                "title": "Title2",
                "description": "<p><a href=\"#\">Beth<\/a> posted a photo:<\/p><img src=\"https://farm8.staticflickr.com/7287/26333398074_cfbce73532_m.jpg\"/>"
            }]
        }
        
        $scope.trustHtml = function(desc) {
        	return $sce.trustAsHtml(desc);
        }
        
    }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MainCtrl">
  
  <h1>{{jsonStr.title}}</h1>
  
  <div ng-repeat="post in jsonStr.items">
    <h3>{{post.title}}</h3>
    <div ng-bind-html="trustHtml(post.description)" />
  </div>
  
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Here I need to extract only <img> tag not the whole description, previous version of answer was helpful
0

Use jqLite, use angular.element(post.description)[0].querySelector('img'); in your link function (if you are using a directive), or where ever angular.element is available to you.

Comments

0

You can use $limitTo filter with a negative value for get the tag and then $sanitize for render it as html.

Read the next docs: https://docs.angularjs.org/api/ng/filter/limitTo

https://docs.angularjs.org/api/ngSanitize/service/$sanitize

Comments

0

AngularJS comes with jqLite, called with angular.element()

obj['items'].forEach(function(val, i){
  var elm = angular.element(val['description'])[1].src;
  console.log(elm);
});

Comments

0

How I would do it:

  • Loop over each item
  • Use regex to get the img url (e.g. \<img src=\\\"(.+)\\\")
  • add the image url (imgUrl in this example) to each item.

Then in your html use it like this:

<article ng-repeat="post in posts">
    <h1>{{ post.title }}</h1>
    <img ng-src="{{ post.imgUrl }}"/>
</article>

Notes

  • I used ng-src, not src
  • The regex is only a quick sample

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.