I have a string json object that I am trying to format and display in the html. I have tried using JSON.parse()
and JSON.stringify()
but the typeof is still showing as a string and it is displaying in a straight line instead of formatting . I also tried <pre> {{formatJson | json}}</pre>
and still no success.
formatJSON: string = '{"a":1,"b":2,"c":{"d":3, "e":4}}'
ngOnInit() {
var test = json.parse(this.formatJSON);
console.log(typeof test); //string
this.formatJSON = JSON.stringify(test, null, " ")
}
HTML Code:
<div>
<textarea [(ngModel)]="formatJSON" class="text-area" cols="200" rows="10">
{{formatJSON}}
</textarea>
</div>
<!-- <pre>{{formatJSON | json}}</pre> -->
Desired Output:
Try this:
var data = {"a":1,"b":2,"c":{"d":3,"e":4}}
document.getElementById("json-textArea").innerHTML = JSON.stringify(data, undefined, 2);
textarea {
width:100px;
height: 150px;
}
<textarea id="json-textArea"></textarea>
Please try this. Update your JSON string like below
formatJSON = {
"a": 1,
"b": 2,
"c": {
"d": 3,
"e": 4
}
}
You can apply angular pipe on formatJSON in your html like this {{formatJSON | json}}
.
I'm using the Facebook API SDK for JavaScript to invite the authenticated user's friends to use the app. To invite the friends, I use the Invitable Friends API, like this: FB.ui({ method: '...
I'm using the Facebook API SDK for JavaScript to invite the authenticated user's friends to use the app. To invite the friends, I use the Invitable Friends API, like this: FB.ui({ method: '...
I created an AFK command for my discord bot but I am struggling to figure out how I would add "[AFK]" into the current users nickname. Below is what I have but this takes the users discord name ...
I created an AFK command for my discord bot but I am struggling to figure out how I would add "[AFK]" into the current users nickname. Below is what I have but this takes the users discord name ...
I'm currently writing an e2e test and I would like to create some classes which abstract certain async tasks for me. In the end I would like to instantiate an object, which let's me chain async ...
I'm currently writing an e2e test and I would like to create some classes which abstract certain async tasks for me. In the end I would like to instantiate an object, which let's me chain async ...
Is Array.from(arguments) worse in some way then Array.prototype.slice.call(arguments) for creating an array of arguments? I haven't seen the former used anywhere, and the latter seems to be the ...
Is Array.from(arguments) worse in some way then Array.prototype.slice.call(arguments) for creating an array of arguments? I haven't seen the former used anywhere, and the latter seems to be the ...