Enhance the appearance of data in Angular using a pipe or alternative method

Currently in my Angular 8 application, I have some data displaying in a div like so:

{"Information":"Information one","Output":[{"address1":"someaddress"},{"address2":"someaddress"},{"address3":"someaddress"},{"address4":"someaddress"}]}

However, it's all in a single line which is not optimal.

This is how the .html file appears:

<div><pre><code>{{ content }}</code></pre></div>

I attempted to format it using {{ content | json }}, but the output looked like this:

"{\"Information\":\"Information one\",\"Output\":[{\"address1\":\"someaddress\"},{\"address2\":\"someaddress\"},{\"address3\":\"someaddress\"},{\"address4\":\"someaddress\"}]}"

Which made it even more difficult to read. So, what steps can I take to make it appear more structured like this:


{
  "Information":"Information one",
  "Output":[
    {
      "address1":"someaddress"
    },
    {
      "address2":"someaddress"
    },
    {
      "address3":"someaddress"
    },
    {
      "address4":"someaddress"
    }
  ]
}

Answer №1

To achieve this, utilize the JSON.stringify method.

In your component.ts file, include the following function:

formatJson(jsonData) {
   return JSON.stringify(jsonData,null,'\t');
}

Instead of using

<div><pre><code>{{ data }}</code></pre></div>
in your template, replace it with:

<div><pre><code>{{ formatJson(data) }}</code></pre></div>

View a live demonstration on Stackblitz: https://stackblitz.com/edit/angular-f9tdhu

Answer №2

Are you looking to display your page in a specific format for printing, or are you just interested in previewing it for your own purposes? Personally, I tend to use PrettyJSON for previews.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to bring in an exported class from a TypeScript file

I have a TypeScript file named foo.ts that contains an exported class called "Foo" export default class Foo{ } I am attempting to import this class into another file within the same directory import {Foo} from './foo'; However, I am encounter ...

MD Autocomplete Component: An Input Inside

Currently, I am working on implementing a feature where users can enter different search terms in an input field within Angular Material's autocomplete component. The issue arises when the user clicks on the input option as it closes the autocomplete ...

What is the best way to utilize one service within another service without the need for providers array or providedIn?

How can I use Service1 inside Service2 without having to declare it as a Singleton by defining it in the providers array of a module or using providedIn in the service definition? Instead, I would like to use it at the component level, similar to how we u ...

Issues with MC-Cordova-Plugin on Ionic and Angular Setup

Recently, I integrated a plugin for Ionic from this repository: https://github.com/salesforce-marketingcloud/MC-Cordova-Plugin After successfully configuring it for iOS, I encountered difficulties on Android where the plugin seems to be non-existent. It ...

What is the process of inserting a sparkline chart into a Kendo Angular grid?

I am attempting to display a bullet chart in the first column of my grid. <kendo-grid-column> <ng-template kendoChartSeriesTooltipTemplate let-value="value"> <div> <kendo-sparkline [data]="bulletData" type="bullet" [ ...

Retrieving data from Node.js within an Angular application

I am currently working on retrieving data from MongoDB and displaying it on my website. However, I am facing an issue in sending the entire fetched object to a specific port (the response) so that I can retrieve it from Angular. I also need to know how to ...

Enhancing TypeScript type definitions for the Response.render() method in Express

Struggling with enhancing the type safety of my Express project by extending the Response.render function. import { Response } from "express"; import { Product } from "../models/Product.interface"; export interface ProductListResponse ...

Anticipated the object to be a type of ScalarObservable, yet it turned out to be an

When working on my Angular project, I utilized Observables in a specific manner: getItem(id): Observable<Object> { return this.myApi.myMethod(...); // returns an Observable } Later, during unit testing, I successfully tested it like so: it(&apos ...

"Discover the power of Next.js by utilizing dynamic routes from a curated list

I am working on a Next.js application that has a specific pages structure. My goal is to add a prefix to all routes, with the condition that the prefix must be either 'A', 'B', or 'C'. If any other prefix is used, it should re ...

Ways to conceal and reveal image and text elements based on array loop output

I am currently working on setting up a questionnaire. The questions and answer options are being pulled from a database using an API. Some of the options include images, with the image link stored in the database. I am trying to find a solution where text ...

The 'data-intro' property cannot be bound to the button element as it is not recognized as a valid property

I've been using the intro.js library in Angular 8 and so far everything has been working smoothly. However, I've hit a roadblock on this particular step. I'm struggling to bind a value in the data-intro attribute of this button tag. The text ...

Understanding Vue.js - encountering the error message "property or method is not defined"

Recently, I've come across an issue that seems to be common among many people, but for some reason, I am unable to find a solution despite looking at similar questions. The problem arises when I use a v-for in a Vue Component and the array value cons ...

Unable to sign out user from the server side using Next.js and Supabase

Is there a way to log out a user on the server side using Supabase as the authentication provider? I initially thought that simply calling this function would work: export const getServerSideProps: GetServerSideProps = withPageAuth({ redirectTo: &apos ...

Issue with Stenciljs custom event not triggering using @Listen decorator

I've been trying to grasp the workings of the custom event emitter. While my mouse events seem to be functioning properly, I'm encountering issues with the custom events. Despite emitting correctly, it appears that the listener is not capturing t ...

Leverage classes from a CommonJS module within a Typescript environment

I am facing an issue with my npm package setup. Here is a snippet from one of the files: 'use strict' module.exports = class TModel { constructor (app) { this.app = app } static schema () { } } I am trying to incorporate this int ...

determining the data type based on the function parameter even when a specific type parameter is provided

Consider this example: type UpdateFieldValue<T extends Record<string, unknown>> = (key: keyof T, value: SomeType) => void The goal is to have SomeType represent the value type of the property (key) within object T, with key being provided t ...

How can HostBinding be used to target a custom directive in order to deliver either a success or error message and show it on

I am incorporating a custom directive to display specific server messages/errors following an http request. For example, in the response or error section, I want to target the custom directive and present the emphasized message. The directive is already e ...

Reactive property in the Vue composition API

Within a Vue 3 project that utilizes TypeScript, there are two properties named locale and content: <script setup lang="ts"> import { computed, ref } from 'vue' import { useI18n } from "vue-i18n" import { Landing, Local ...

Component in Angular 5 is experiencing compatibility issues with Angular Material

I have integrated angular material into my Angular 5 application successfully in the app.module.ts file. However, when I try to use it in my page.component.html file, I encounter an error that says: 'Uncaught Error: Template parse errors: 'mat-t ...

Issue with Angular polyfill in IE11: The core-js version 3.6.5 method es.string.split.js is having trouble parsing the regex /^|s+/ when used with

Angular 10, d3 5.16.0, and core-js 3.6.5 In the midst of it all, d3-drag triggers d3-dispatch, which in turn invokes a function called .parseTypenames. function parseTypenames(typenames, types) { return typenames.trim().split(/^|\s+/).map(functio ...