简体   繁体   中英

Angular2 - Can't bind to 'customer' since it isn't a known property of 'my-customer'

I am following an outdated John Papa and Buddy Ward tutorial and cannot get past this issue. I went through a bunch of SO posts that led me to not use directives: in my component and instead use the declarations: property in the main module. Still no luck though :( Below is the console error and the code it is referring to. Can someone please lend a hand? Thank you!

customer.component.ts

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

@Component({
    selector: 'app-customer',
    templateUrl: 'app/customer/customer.component.html'
})

export class CustomerComponent implements OnInit {
    @Input() customer: {id: number, name: string};

    constructor() { }

    ngOnInit() { }
}

customer.component.html

<span [style.color]="deansColor">{{customer.id}}</span>
        &nbsp;
<span>{{customer.name}}</span>

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';
import { AppComponent }  from './app.component';
import { CustomerComponent } from "./customer/customer.component";

@NgModule({
  imports:      [ BrowserModule, FormsModule ],
  declarations: [ AppComponent, CustomerComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

app.component.ts

import { Component } from '@angular/core';
import { CustomerComponent } from './customer/customer.component';

@Component({
  selector: 'my-app',
  templateUrl: 'app/app.component.html'
})
export class AppComponent  { 

  title = "Welcome to Dean's World of Angular..."; 
  heading = 'Enjoy your stay!'
  deansColor = 'red';
  customers = [
    {id: 1, name: "Dean"},
    {id: 2, name: "Daryl"},
    {id: 3, name: "Lee"},
    {id: 4, name: "Josh"},
    {id: 5, name: "Gerald"},
  ];

  changeHeadingColor() {
    this.deansColor = this.deansColor === 'blue' ? 'red' : 'blue';
      }

}

app.component.html

<h1>{{title}}</h1>

<!--property binding-->
<div [style.color]="deansColor">
    {{heading}}
</div>

<!--event binding-->
<button (click)="changeHeadingColor()"> 
    Change Heading Color
</button>

<!--two way data-binding (approach #1)-->
<input type="text" [value]="heading" (keyup.enter)="heading = $event.target.value"> 

<!--two way data-binding (approach #1)-->
<input type="text" [(ngModel)]="heading">

<br/>

<ul>
    <li *ngFor="let c of customers">     
        <my-customer [customer]="c"></my-customer>   
    </li>
</ul>

Error

Unhandled Promise rejection: Template parse errors:
Can't bind to 'customer' since it isn't a known property of 'my-customer'.

You're using the wrong selector.

Try:

<app-customer [customer]="c"></app-customer>

Change selector: 'app-customer' , in customer.component.ts to selector: 'my-customer' . PS I think <span [style.color]="deansColor">...</span> should've change color as deansColor is defined in the parent component only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM