Having difficulty transferring data between components using @Input syntax

I am having trouble passing the FailedProductId from Component1 to Component2 using @Input. Below is the code snippet:

export class Component1 implements OnInit {
  public FailedProductId="produt";
  constructor(private employeeService: ProductService) {}
}

The .html file of Component1:

<div> 
 <app-device-list [parentData]="FailedProductId"></app-device-list>

  <table>
    <th>Id</th><th>Active</th><th>Name</th><th>Platform</th><th>ManagedBy</th>
    <th>Compliant</th> <th>InProgress</th><th>Failed</th>
    <tr *ngFor="let lst of products.Products; let i = index" border="1">
      <td>{{lst.GroupName}}</td>
      <td (click)="devicesClicked()">{{lst.Compliant}}</td>
      <td>{{lst.InProgress}}</td>
      <td><a routerLink="/devicelist/{{lst.ID.Value}}">{{lst.Failed}}</a></td>
    </tr>
  </table>   
</div>

Component2.ts file content:

@Component({
  selector: 'app-device-list',
  template: '<h2>{{"Hello "+ parentData}}</h2>'
})

export class Component2 implements OnInit {

  @Input() public parentData;
}

The line

'<h2>{{"Hello "+ parentData}}</h2>'
outputs Hello undefined.

Answer №1

In order to properly configure your second component, make sure it is set up as follows: Moreover, verify that the data is being correctly set in the first component.

@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  selector: 'app-device-list',
  template: ' <h2>{{"Hello "+ parentData}}</h2> '
})

export class Component2 implements OnInit {

  @Input() public parentData;

  }

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

Implement a class in Typescript that allows for the addition of properties at runtime

I'm currently in the process of incorporating Typescript definitions into an existing codebase that utilizes the Knockout library. Within the code, there is a prevalent pattern that appears as follows: interface SomeProperties { // Assorted prope ...

The 'ngForOf' property cannot be bound to 'tr' as it is not recognized. Make sure to import CommonModule and BrowserModule

Encountering a strange issue. When attempting to use *ngFor on a tr element, an error is thrown: <table> <tbody> <tr *ngFor="let item of productsData"> <!-- some Code --> <tr> </tbody> </table> Within my ...

Whenever I execute the 'ng serve' command, I encounter an issue with ineffective mark-compacts close to the heap limit, resulting in an allocation failure and a JavaScript

I'm currently using Angular 9 and Node.js 12. When I input ng serve, I encounter the following problem: C:\Users\homz\my-app>ng serve 93% after chunk asset optimization SourceMapDevToolPlugin vendor.js generate SourceMap <--- ...

Converting md ElementRef to HtmlElement in Angular 2+: A Step-by-Step Guide

My query is related to retrieving the favorite food input in TypeScript. The input field is defined as: <input mdInput #try placeholder="Favorite food" value="Sushi"> In my TypeScript file, I have accessed this input using: @ViewChild('try ...

Clarification Needed on Keyup Event in Angular 2

Within my application, I am dynamically adding a class based on certain conditions. When the user inputs something, I validate the value and then add the appropriate class accordingly. This functionality is functioning as expected. However, the class upda ...

Upgrading to Angular 2: Utilizing ElementRef in ES5

Currently, I am facing a challenge in creating an Attribute directive for Angular 2 that would allow me to set multiple default HTML attributes using a single custom attribute. My intention is to apply this directive specifically to the input element. Howe ...

"Unleash the Power of Go HTTP Server for React, Angular, and

Recently, I developed a small HTTP Server in GO specifically for static files: func wrapHandler(h http.Handler) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { h.ServeHTTP(srw, r) log.Printf("GET %s", r.RequestU ...

Tips for simulating localStorage in TypeScript unit testing

Is there a method to simulate localStorage using Jest? Despite trying various solutions from this post, none have proven effective in TypeScript as I continue encountering: "ReferenceError: localStorage is not defined" I attempted creating my ...

Understanding a compound data type in TypeScript

Hey there, I'm new to TypeScript and I'm facing a challenge in defining the type for an object that might have the following structure at runtime: { "animals" : [ { name: "kittie", color: "blue" }, { name: ...

Dealing with a section that won't stay in place but the rest of the webpage is

I recently came across the angular-split library while trying to address a specific need. It partially solved my problem, but I still have some remaining challenges. In my setup, I have divided my content into 2 sections using angular-split. The goal is f ...

Error message in Docker: "Command /bin/sh: 1: ng: not found; please make sure Angular

While attempting to create a docker image for my Angular app, I encountered an issue where it crashes on RUN ng build --prod and the error message displayed is: /bin/sh: 1: ng: not found The command '/bin/sh -c ng build --prod' returned a non-ze ...

Using AsyncPipe within an ngFor loop to display items

What is the best practice for using an AsyncPipe within an ngFor loop to handle asynchronous calls for items in a list? Scenario: Imagine I have an Order object with Products, and I need to display each product's manufacturer. However, the manufactur ...

Adding content in real-time at the current cursor position within a Contenteditable element using Angular

I have a contenteditable div where I am facing an issue with pasting text. Whenever I try to paste some text into the div, it always gets pasted at the end. I am using ViewChild to access the reference of the contenteditable div and utilizing innerText to ...

Understanding Typescript in Next.js components: Deciphering the purpose behind each segment

Consider the following function: type User = { id: string, name: string } interface Props { user: User; } export const getUserInfo: GetUserInfo<User> = async ({ user }: Props) => { const userData = await fetchUser(user.id); return ...

Adding an anchor tag to an ngx-datatable-column can be done by utilizing the properties

My task involves loading data from the server and populating the ngx-datatable. When a specific column is clicked (with either a link <a href="randomurl"/> or [routerLink]="randomcomponent"), it should redirect to a different page or display a modal ...

Bringing a JavaScript file into an Ionic/Angular 2 project

I have been attempting to integrate a simple JS library into Angular 2. The library in question is JIC.js. var jic = { /** * This function takes an Image Object (JPG or PNG) and returns a compressed new Image Object * @param {Ima ...

Change the spread operator in JavaScript to TypeScript functions

I'm struggling to convert a piece of code from Javascript to Typescript. The main issue lies in converting the spread operator. function calculateCombinations(first, next, ...rest) { if (rest.length) { next = calculateCombinations(next, ...res ...

Is there a way to convert a typescript alias path to the Jest 'moduleNameMapper' when the alias is for a specific file?

I am currently working on setting up Jest in a TypeScript project. In our tsconfig.json file, we are using path aliases like so: "baseUrl": ".", "paths": { "@eddystone-shared/*": [ "../shared/*" ], "@eddystone-firebase-helpers/*": [ "src/helpers/fire ...

Tips for implementing md-icon in combination with md-autocomplete in Angular Material Design:

How can I include an md-icon in an md-autocomplete field? <md-autocomplete md-selected-item="ctrl.selectedItem" md-search-text-change="ctrl.searchTextChange(ctrl.searchText)" md-search-text="ctrl.searchText" md-items="it ...

Angular CLI fails to generate angular-cli.json file

Currently in the process of creating a new PWA and I need to input information into angular-cli.json. Utilizing Angular CLI: 1.7.4 Node: 8.11.1 OS: win32 x64 Angular: 5.2.10. Upon running ng new pwaapp --service-worker, the project folder does not conta ...