angular directive to receive an object

When I have a table and click on a row, the information is supposed to be displayed in a different component using the @input decorator. However, instead of displaying the correct result in my other component, I am getting [object Object].

table.html

<tr mat-row *matRowDef="let row; columns: displayedColumns;" (click)="selected(row)"></tr>
</table>

table.ts

 public selectedArrayParent!: Product;
     selected(row:Product) {
        this.selectedArrayParent = row;
        console.log(this.selectedArrayParent);
      }

info.ts

 @Input() public selectedArrayEnfant!: Product;

info.html

{{selectedArrayEnfant}}

Answer №1

You cannot directly view objects in HTML, you need to stringify them. I have provided an example link for better understanding.

To address this, I have created a demo pipe that converts objects into strings:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'stringify'
})
export class StringifyPipe implements PipeTransform {

  transform(value: any): any {
    return JSON.stringify(value);
  }

}

In the HTML code, I have included both the object and its string representation:

{{selectedArrayChild}}
 {{ selectedArrayChild | stringify}}

The output will be as follows:

[object Object]
{"test":"test"}

Answer №2

If you are working with code that takes an object type input parameter, don't forget to convert the object into a string before displaying the result. To achieve this, consider using

console.log(JSON.stringify(this.selectedArrayParent))
within the selected function to obtain the JSON data in a string format. For more information on formatting JSON objects for HTML display, check out this link.

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

Issue with Adding Class to Angular2 Material Tooltips

Here is the code from my component.html file: <tr> <td> <span matTooltipClass="primary-tooltip" matTooltipPosition="above" matTooltipHideDelay="100000" matTooltip="{{cert.awsCertId}}"><p style="overflow:hidden;text-overflow: ellip ...

Angular recognizing string-array type as a string input is not correct

I am encountering a challenge with an Angular CLI component that involves working with an array of strings called "searchResult": export class ParentComponent implements OnInit { mockArray: string[] = []; searchString: string = ''; searchR ...

Navigating through child routes in Angular can be tricky, especially when dealing with unknown routes

I'm having some trouble with routing today. I have a collection of components each associated with a unique route. I am trying to include a 'PageNotFoundComponent' for any incorrect route in both the parent and child components. Let me expla ...

Communicating data between Angular components that have no direct binding or relationship

Struggling to transfer data between two unrelated components, anyone have advice on how to accomplish this? Here's an example: I have a page with 3 buttons that pass string values upon click to a variable named selectAgent. ~agents.html~ <div rou ...

"I am looking for a way to incorporate animation into my Angular application when the data changes. Specifically, I am interested in adding animation effects to

Whenever I click on the left or right button, the data should come with animation. However, it did not work for me. I tried adding some void animation in Angular and placed a trigger on my HTML element. The animation worked when the page was refreshed, bu ...

Should I implement jquery within angular 2, or opt for the current css/js frameworks available?

Embarking on the development of a website with Angular 2, my deadline is set within a month. I need to select a CSS/JS framework that seamlessly integrates with Angular 2. While using jQuery within Angular 2 is discouraged, I am curious if there are altern ...

Terminate all active service requests and retrieve only the outcomes from the most recent request

Is there a way to cancel ongoing requests and only retrieve the result of the latest request triggered by my service on the backend? Here's my current code: this.vehiclesService.getVehiclesByPage(currentState).subscribe(success => { this.c ...

Incorporating an expansion panel within an Angular Material table row

I'm currently working on incorporating an expansion panel, possibly a new component, similar to the mat-accordion. This will allow for a detailed view to be displayed within a mat-table row upon clicking. To better illustrate this requirement, I have ...

Is it possible to overlook TypeScript errors when compiling with Angular 2 AoT?

My application is experiencing numerous TypeScript errors, even though it runs correctly. I recently migrated a JavaScript app to TypeScript and am struggling to resolve all the type-related issues. In order to proceed with development, I have configured m ...

Learn how to trigger an event or API call in Angular 8 when the browser is closed with the help of HostListener

I am facing the challenge of calling a simple websocket closure API when the browser is closed in my project. I attempted to utilize HostListener, but unfortunately it did not work as expected. You can find the code snippet below: https://stackblitz.com/ ...

Prisma: Incorrectly identifying existing items where the list contains any of the specified items

Within my Prisma model, I have a property designated to store a list of phone numbers in the format phones String[] @unique When making an API call with a model that may include one or more phone numbers, my goal is to locate any existing record where any ...

Display a React functional component

Greetings, friends! I recently created a React app using functional components and now I am looking to print a specific page within the app. Each page is its own functional component, so I was wondering if it's possible to print a component individual ...

Is it possible to display the combined string when concatenating two strings?

Consider the following code snippet: const a = "url"; const b = "/route"; const c = a + b; Even though TypeScript knows the exact values of a and b, resulting in immutable constants, when concatenating both strings, the type of c is di ...

Struggling with setting up a search bar for infinite scrolling content

After dedicating a significant amount of time to solving the puzzle of integrating infinite scroll with a search bar in Angular, I encountered an issue. I am currently using Angular 9 and ngx-infinite-scroll for achieving infinity scrolling functionality. ...

Is it possible to enable typescript to build in watch mode with eslint integrated?

Can this be achieved without relying on webpack or other bundlers? Alternatively, is the only solution to have two separate consoles - one for building and another for linting? ...

Angular is facing a challenge in locating the main parent based on its class interface

After reading the angular documentation here, I implemented a Parent class like this: export abstract class Parent {} In the AlexComponent, I set this component as the Parent for its children with the following code: providers: [{ provide: Parent, useExis ...

Issues with Angular ng-bootstrap tabset component not functioning as expected

{ "name": "ModalWindow", "version": "1.0.0", "repository": { "type": "git", "url": "" }, "scripts": { "build": "webpack --mode production", "start": "webpack-dev-server --mode development --open" }, "license": "MIT", "depend ...

Encountered a runtime error while processing 400 requests

Current Situation: When authenticating the username and password in my Ionic 2 project using WebApi 2 token authentication, a token is returned if the credentials are correct. However, a 400 bad request error is returned if the credentials are incorrect. ...

Is it possible to dynamically insert additional fields when a button is clicked?

My FormGroup is shown below: this.productGroup = this.fb.group({ name: ['', Validators.compose([Validators.required, Validators.maxLength(80)])], desc: ['', Validators.maxLength(3000)], category: ['', Validators.require ...

The pagination button in angular bootstrap UI caused the button to shift when clicked

I am currently using angular bootstrap's UI pagination to display results with next and previous buttons. The result sets are displaying correctly, however, I am encountering an issue when clicking on the pagination buttons. Specifically, the buttons ...