简体   繁体   English

angular2上的input()无法正常工作

[英]input() on angular2 is not working Properly

first of all this is my GIT repository where you can see the code: 首先,这是我的GIT存储库,您可以在其中查看代码:

https://github.com/eshk12/myDogs https://github.com/eshk12/myDogs

I try to practice some parts from the lesson so I have create ANG2 Project call "myDogs". 我尝试练习本课中的某些部分,因此创建了名为“ myDogs”的ANG2项目。

in the porject I have 3 components,root and DogList,dog. 在项目中,我有3个组件,root和DogList,dog。

i have an issue in "DogList" component when I try to send the value "currentDog" as "dogC" to the component "Dog", and error came up and said: 当我尝试将值“ currentDog”作为“ dogC”发送到组件“ Dog”时,我在“ DogList”组件中遇到问题,出现错误并说:

Unhandled Promise rejection:

Template parse errors:
Can't bind to 'dogC' since it isn't a known property of 'dog'. ("
  <div>
    <dog *ngFor="let currentDog of dogs"
            [ERROR ->][dogC]="currentDog"
    >
    </dog>
"): DogList@3:12

; Zone: <root> ; Task: Promise.then ; Value: BaseException { message="Template parse errors:\nC...</dog>\n"): DogList@3:12",  stack="BaseException@http://loc...js/dist/zone.js:426:22\n",  constructor=BaseException(),  more...}
zone.js (line 461)
Error: Uncaught (in promise): Template parse errors:
Can't bind to 'dogC' since it isn't a known property of 'dog'. ("
  <div>
    <dog *ngFor="let currentDog of dogs"
            [ERROR ->][dogC]="currentDog"
    >
    </dog>
"): DogList@3:12
throw new Error("Uncaught (in promise): " + value);

I tried to "ul li"ed it like that: *ngFor="let dogC of dogs" and its work, 我试图像这样“ ul li”:* ngFor =“ let dogC of dogs”及其工作,

but when I send it to the other component error come up. 但是当我将其发送到其他组件时出现错误。

The original GIT repository from my teacher that work: https://github.com/rotemx/Class4-MyMusic 我老师工作的原始GIT储存库: https//github.com/rotemx/Class4-MyMusic

Thanks in advance to those who will help me find the problem. 在此先感谢那些将帮助我发现问题的人。

In your Dog component: 在您的Dog组件中:

export class Dog {
  @Input() public DogC: DogModel;
}                 ^^^^

In your DogsList component template: 在DogsList组件模板中:

<dog *ngFor="let currentDog of dogs"
        [dogC]="currentDog"
>       ^^^^^^
</dog>

You defined the Input as DogC but you are trying to bind to dogC . 您将Input定义为DogC但是您试图绑定到dogC Javascript/Typescript is case-sensitive. Javascript / Typescript区分大小写。 Change the Input DogC to dogC and it will work. 将输入DogC更改为dogC ,它将起作用。

By the way, it is not recommended to keep the templates of components in separate folders from the components themselves. 顺便说一句,不建议将组件模板与组件本身放在单独的文件夹中。 Check out the official Angular 2 style guide for the recommended file structure and naming conventions. 查看官方的Angular 2样式指南 ,了解建议的文件结构和命名约定。

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

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