Question

I have an array called employees in my Angular2 component and I want to see what data it has. If I do something like this:

<p>{{ employees }}</p>

it just displays [object Object]

Is there any Angular2 way that lets me quickly display the value it contains?

1 Answers

Use JsonPipe in your HTML expression. It applies JSON.stringify() to your data and display its JSON representation properly.

To use a pipe, you have to add the | (pipe) character after your expression followed by the name of the pipe you want to use. The resultant data of the expression is passed through the pipe and then the specified pipe is applied to it thereby transforming the data.

<p>{{ employees | json }}</p>

The resulting HTML will be like this:

<p>[ { "name": "Emp 1" }, { "name": "Emp 2" } ]</p>