You can try with |json
<div class="box box-default">
<code>
<pre>{{placeDetail |json}}</pre>
</code>
</div>
Answer from Ketan Akbari on Stack OverflowYou can try with |json
<div class="box box-default">
<code>
<pre>{{placeDetail |json}}</pre>
</code>
</div>
For native JS: : <script>document.write(JSON.stringfiy(placeDetail )</script>
For angular you need to add the <code> tag
Angular:
<code>
<pre>{{placeDetail |json}}</pre>
</code>
Native JS:
<div class="box box-default">
<pre><script>document.write(JSON.stringify(placeDetail))</script></pre>
</div>
Videos
If you want to see what you you have inside an object in your web app, then use the json pipe in a component HTML template, for example:
<li *ngFor="let obj of myArray">{{obj | json}}</li>
Tested and valid using Angular 4.3.2.
We can use angular pipe json
{{ jsonObject | json }}
In your template use the pre tag and json pipe,
<pre> {{ InstanceId | json }} </pre>
1. Concatenate string and object in controller
You need to use comma instead of plus to log the object in the controller.
console.log("InstanceId is: ", InstanceId)
instead of
console.log("InstanceId is: " + InstanceId)
2. Render object property in the template
The hyphen in the property must obfuscate for the template rendered to access it. You could use bracket notation instead of dot notation here.
<td>{{ Instance Id["case-id"]}}</td>
instead of
<td>{{ Instance Id.case-id}}</td>
You can use the json pipe. In your template:
<pre>Data Obtained is: {{ data | json }}</pre>
Also you have to change the type of your data property to any instead of string.
You have two options:
Use the built-in pipe JsonPipe (
this.datashould be of typeany):<pre>Data Obtained is: {{ data | json }}</pre>Get the JSON string manually:
this.data = JSON.stringify(res.json()); //data is a string :)
or
`<pre>Data Obtained is: {{ JSON.stringify(data) }}</pre>`
You have to understand that any value in a template is shown via calling its .toString() method, so a basic object (something like {key: value}) just shows [object Object]
Angular has a Json filter you can add to the actual binding expression, once you have the json back.
{{ answer | json }}
If you want the actual json from the response, you can find it in the data property of the response object.
response.data
Improvement suggestion:
I've also provided a 'nicer' short hand for your http get method which I think its actually better because it'll handle any exceptions that gets thrown rather than using an error callback in your case.
return $http.get(apiUrl)
.then(successCB)
.catch(errorCB);
You are not assigning any data to the answer (actually assigning undefined) because findPlayer doesn't return anything.
So first of all, you need to make service method return promise object:
this.findPlayer = function() {
var url = 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/Crucify?api_key=222015c4-0898-4f6b-a7d5-2a23c3e0344d';
return $http({
method: 'GET',
url: url
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
return response.data;
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
return response;
});
};
then consume it in controller:
$scope.search = function () {
personSearch.findPlayer().then(function(data) {
$scope.answer = data;
});
}