0

I want to refacto my code. I have this ngFor in my section-portrait.component.html

<app-portrait *ngFor="let model of models"
      [firstName]="model.firstName"
      [lastName]="model.lastName"
    ></app-portrait>

Here is my portrait.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-portrait',
  templateUrl: './portrait.component.html',
  styleUrls: ['./portrait.component.scss']
})
export class PortraitComponent implements OnInit {
  firstName: string;
  lastName: string;

  constructor() {
  }

  ngOnInit() {
  }
}

And my portrait.component.html

<h4>
  <a href="#">{{ firstName }} {{ lastName }}</a>
</h4>

I want to loop on every Model to display there firstName and lastName

And I have this error:

Uncaught Error: Template parse errors: Can't bind to 'firstName' since it isn't a known property of 'app-portrait'.

What did I do wrong?

2
  • 1
    They need to be inputs - @Input() firstName: string, @Input() lastName:string Commented Nov 1, 2018 at 14:33
  • 1
    Make firstName, lastName as Input properties Commented Nov 1, 2018 at 14:33

1 Answer 1

1

There's nothing wrong with your ngFor. Congratulations!

However, your Component's properties are specified incorrectly. If you want to inject them with values from your HTML, you'll need to expose them through @Input.

import { Component, Input, OnInit } from '@angular/core';

@Component({
  selector: 'app-portrait',
  templateUrl: './portrait.component.html',
  styleUrls: ['./portrait.component.scss']
})
export class PortraitComponent implements OnInit {
  @Input() firstName: string;
  @Input() lastName: string;

  constructor() {
  }

  ngOnInit() {
  }
}

If you wanted, you might be interested in going one step further:

@Input() model: Model;

<app-portrait *ngFor="let model of models" [model]="model"></app-portrait>
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.