0

I am getting a JSON Reponse from an api in AngularJS. I have description in it .Description contains HTML Tags in string. I want to share Response item here.

{
  name:'Name of Item',
  Description:'<div>Details: Here is Details</div><div>Type: Here is Type</div>'
}

I just want to separate details and Type from description into two different strings. any body can help. I tried to search some help, but did not get any +ive or match result for this.

Thanks

1

3 Answers 3

5

Parse the HTML string by inserting it into a dummy HTMLElement. Then you can access the text of the nodes by accessing the textContent of the childNodes.

var data = {
  name: 'Name of Item',
  Description: '<div>Details: Here is Details</div><div>Type: Here is Type</div>'
};

//Create a dummy element to hold the HTML
var el = document.createElement('div');
el.innerHTML = data.Description;

//Get child nodes; can use getElementById, querySelectorAll, etc.
var details = el.childNodes[0].textContent;
var type = el.childNodes[1].textContent;

console.log(details);
console.log(type);

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

Comments

1

may be you need to do some string manupulation. Just like below.

var app = angular.module('app', [])
.controller('ctrl', function ($scope) {
    var data = {
        name: 'Name of Item',
        Description: '<div>Details: Here is Details</div><div>Type: Here is Type</div>'
    }

    $scope.data = data;
    $scope.result = $scope.data.Description.split('</div>');
    $scope.result = $scope.result[0].slice(0, -1); //take the first value from array, and slice last char, which is comma
    $scope.result = $scope.result + '</div>' //insert div at the end
})

1 Comment

I implemented this, but this is not a professional way, BTW I like it :) @Rakesh Burbure
0

first extract the description part from json.

Let's say your json is stored in

    var response = API call();
    response.data.Description.split('</div>')

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.