What is the best way to assign TypeScript interfaces to an Input field?

Suppose I have the following interface CardDetail:

export interface CardDetail {
   name: string;
   type: string;
   logo?: string;

What is the correct way to ensure that the @Input() decorators accept this type of data?

import { CardDetail } from '../card-detail'

@Input cardDetail: CardDetail
export class CardComponent {
}

When trying to implement this, I encounter the error: 'CardDetail' only refers to a type, but is being used as a value here.ts(2693)

Is there an alternative approach I can take? In my parent class, I specify:

carddetails = CardDetail[]

constructor() {
 this.carddetails = [
      { name: "foo", logoUrl:'/assets/foo.png', type: "bar" } 
      ...
 ]

and pass these values in an *ngFor loop in the template

<ng-template *ngFor="let cardDetail of carddetails">
    <app-card [cardDetail]="cardDetail"></app-card>
</ng-template>

Answer №1

@Input needs to be properly placed within the component class. Don't forget to include () after @Input. Your code should look like this:

export class CardComponent {
   @Input() cardDetail: CardDetail
}

Instead of:

@Input cardDetail: CardDetail
export class CardComponent {
}

If you are passing an array from the parent class, then make sure to adjust your code as follows:

@Input() cardDetail: CardDetail[];

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

Emitter fails to emit the value

I've been working on a filter component that looks like this: <app-filter (newStatusValue)="changeListByStatus($status)" However, I'm running into an issue where nothing is being output... changeListByStatus($status){ console.log('ch ...

Guide for setting up various validations for formGroup in an Angular 5 project

I am currently working on implementing Angular reactive form validation. My existing structure only includes validation for required fields, but I am looking to add additional validations such as minLength, email, and postcode. One challenge I face is cre ...

What steps are involved in creating animations on angular.dev?

I'm trying to recreate the animation on angular.dev. Specifically, I want to create a zoom effect on the Angular logo when scrolling. However, I'm unsure how to achieve this effect. If you visit this page , you'll see exactly what I mean. ...

Guide on integrating a plain Service/Provider into nest.js

I recently created a basic TypeScript class in nest.js called JwtTokenService.js. // JwtTokenService.js import { Injectable, Optional } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { JwtPayload } from ' ...

The .angular-cli.json file is causing issues with loading scripts

I have recently updated my .angular-cli.json file by adding new scripts in the following format: "apps": [ { "root": "src", "outDir": "dist", "assets": [ "assets", "favicon.ico" ], "index": "index.html ...

Is it possible for Angular 2 JWT to have an unauthenticatedRedirector feature?

Having transitioned from Angular 1 where JWT tokens were used for user authentication, I had the following code: .config(function Config($httpProvider, jwtOptionsProvider) { // Interceptor to add token to every $http request jwtOptionsProv ...

Turn off the page refresh in Angular 6 routing

Having an issue with Angular where clicking a link causes the browser to reload the entire site instead of just loading the component associated with the path. What should I do? The default components are Page1 and Page2. In app.module.ts: import {Router ...

The mat dialog is displayed with a mat table below the title line

Is there a solution for the issue where the mat-table in a mat dialog displays a line below the heading? This is the code snippet: <h2 mat-dialog-title>dialog title</h2> <mat-dialog-content class="mat-typography"> <div c ...

What is the solution for the error stating "Unable to locate a declaration file for the module 'request-context'."?

I am currently working on three different files: index.js, index.main.js, and app.js. My goal is to use request-context to extract a variable from index.main.js and pass it to index.js. Within my app.js file, located in the server folder, I have the follo ...

The application component seems to be stuck in a loading state and is not appearing as expected on my index.html

Click here to view the Plunkr <html> <head> <base href="/"> <title>Angular QuickStart</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <l ...

Tips for utilizing the window object in Angular 7

To implement the scrollTo() function of the window object directly, we can use window.scrollTo(0,0). However, when researching how to do this in Angular, I found that many people create a provider as shown below: import {InjectionToken, FactoryProvider} f ...

Tips for personalizing the Material UI autocomplete drop-down menu

I'm currently working with Material UI v5 beta1 and I've been attempting to customize the Autocomplete component. My goal is to change the Typography color on the options from black to white when an item is selected. However, I'm struggling ...

The element 'router-outlet' in Angular 9.0 is not recognized within a lazily loaded module

My attempt at adding a new lazyloaded module to the app component is running into an issue. When I try to include child routes for the new module, an error stating that 'router-outlet' is not a known element:in lazy loaded module is thrown. In t ...

How can I implement a dynamic progress bar using Ionic 4?

I am currently working on developing a progress bar that increases by 1% every time the user clicks a button. This is what my HTML code looks like: <ion-button *ngFor="let progress of progress" (click)="add(progress)">Progress</ion-button> &l ...

The table component in Primeng is encountering issues when being used with ngFor

I'm attempting to iterate through an array where each object represents a table in HTML, and it should be displayed like this: <p-table [value]="section" *ngFor="let section of sections"> <ng-template pTemplate="header"> <t ...

Choose a file in React by specifying its path instead of manually picking a file

Is there a way for me to automatically select a file from a specified path into my state variable without having to open a select file dialog? I'm looking for a solution where I can bypass the manual selection process. Any suggestions on how this can ...

flickering effect problem

What could be causing the issue where [@fadeInOut] only works on the initial page load when toggling isExpanded to hide or show content? Due to [@fadeInOut], the content stops showing up. Any thoughts on what might be causing this and any alternative solut ...

What is the process for initiating a local Lambda edge viewer request?

Is there a way to run aws cloudfront lambda edge functions locally and simulate the event in order to observe the response from one of the four functions? I made modifications to the viewerRequest function of lambdaEdge, but I'm wondering if there is ...

Expanding ngFor in Angular 2

Is it possible to pass two arguments with ngFor? Here is an example that I would like to achieve: <mat-card *ngFor="let room of arr; let floor of floorArr"> <mat-card-content> <h3>Room Number: {{room}}</h3> <p>Floor ...

Enhancing angular component with aria-label attribute

I have created a star rating component and I want to include a description for screen readers like JAWS to read when tabbed. Currently, when using JAWS, there is no description being read (it should ideally read the rating for the user). What is the proces ...