简体   繁体   English

Angular2 - 无法绑定到“客户”,因为它不是“我的客户”的已知属性

[英]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.我正在关注过时的 John Papa 和 Buddy Ward 教程,无法解决这个问题。 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.我浏览了一堆 SO 帖子,这些帖子导致我没有在我的组件中使用directives:而是在主模块中使用declarations:属性。 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客户.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客户.component.html

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

app.module.ts 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 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应用程序组件.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' .selector: 'app-customer'更改为selector: 'my-customer' PS I think <span [style.color]="deansColor">...</span> should've change color as deansColor is defined in the parent component only. PS 我认为<span [style.color]="deansColor">...</span>应该改变颜色,因为 deansColor 仅在父组件中定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 7:无法绑定到“元素”,因为它不是 - Angular 7 : Can't bind to 'element' since it isn't a known property of 无法绑定到“”,因为它不是“ angular 2”的已知属性 - can't bind to '' since it isn't a known property of '' angular 2 无法绑定到“appIfRoles”,因为它不是“p”的已知属性 - Can't bind to 'appIfRoles' since it isn't a known property of 'p' 无法绑定到“ chartType”,因为它不是“ canvas”的已知属性 - Can't bind to 'chartType' since it isn't a known property of 'canvas' 错误:无法绑定到“ ngModel”,因为它不是的已知属性 - Error: Can't bind to 'ngModel' since it isn't a known property of 无法绑定到“已禁用”,因为它不是&#39; <button>&#39;</button>的已知属性 - Can't bind to 'disabled' since it isn't a known property of '<button>' 无法绑定到“highlight”,因为它不是“code”的已知属性 - Can't bind to 'highlight' since it isn't a known property of 'code' 带有 Angular 的 NG2 图表:无法绑定到“颜色”,因为它不是 Angular 中“画布”的已知属性 13 - NG2 Chart with Angular: Can't bind to 'colors' since it isn't a known property of 'canvas' in Angular 13 无法绑定到“控件”,因为它不是(myComponent)的已知属性 - Can't bind to 'control' since it isn't a known property of (myComponent) 无法绑定到“ useStickyClasses”,因为它不是“ div”的已知属性 - Can't bind to 'useStickyClasses' since it isn't a known property of 'div'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM