1

I am trying to write a very simple program of passing value from parent to child component. In the below code, I am trying to print firstName(John) but as shown in the screenshot, it's not getting printed.

app.component.ts

import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { firstName = "John"; } 

app.component.html

<app-cards> [firstName]="firstName" </app-cards> 

card.component.ts

 import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'app-cards', templateUrl: './cards.component.html', styleUrls: ['./cards.component.css'] }) export class CardsComponent implements OnInit { @Input() firstName : string | undefined; constructor() { } ngOnInit(): void { } } 

card.component.html

<div> First Name: {{ firstName }} </div> 

Output :

enter image description here

1 Answer 1

1

Your app.component.html file is wrong. You have [firstName] between the <app-cards> element tags instead of having it as an attribute.

app.component.html

<app-cards [firstName]="firstName"> </app-cards> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.