Angular2例外:无法绑定到'ngForIn',因为它不是一个已知的原生属性 [英] Angular2 exception: Can't bind to 'ngForIn' since it isn't a known native property

查看:5809
本文介绍了Angular2例外:无法绑定到'ngForIn',因为它不是一个已知的原生属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么错了?

 进口{引导,组件}从'angular2 / angular2@零件({
  选择:CONF会谈,
  模板:`< D​​IV * ngFor =#会谈中谈>
     {{talk.title}}通过{{talk.speaker}}
     &所述p为H.; {{talk.description}}
   < / DIV>`
})
类ConfTalks {
  会谈= {[标题:T1,主讲人:'布莱恩',说明:讲1},
            {标题:T2,主讲人:朱莉,说明:讲2'}];
}
@零件({
  选择:我的应用,
  指令:[ConfTalks]
  模板:'<的conf会谈>< / conf目录会谈>'
})
类应用{}
引导程序(App,[])

该错误是

 例外:模板解析错误:
不能绑定到ngForIn',因为它不是一个已知的原生属性
(< D​​IV [错误 - >] * ngFor =谈判#谈>


解决方案

由于这是至少我已经浪费了这个问题,我想我会发布Q&放超过5分钟,第三次;答:我希望它可以帮助别人的道路......大概我!

我在ngFor前pression键入,而不是。它应该是:

 < D​​IV * ngFor =#会谈的谈话>


注意,ngFor语法desugars到以下内容:

 <模板ngFor #talk [ngForOf] =谈判>
  < D​​IV> ...< / DIV>
< /模板>

如果我们使用相反,它变成

 <模板ngFor #talk [ngForIn] =谈判>
  < D​​IV> ...< / DIV>
< /模板>

由于 ngForIn 不是具有相同名称的输入属性的属性指令(如 ngIf ),角然后尝试,看看它是否是一个(称为本机)在模板元素的属性,它不是,因此错误。

What am I doing wrong?

import {bootstrap, Component} from 'angular2/angular2'

@Component({
  selector: 'conf-talks',
  template: `<div *ngFor="#talk in talks">
     {{talk.title}} by {{talk.speaker}}
     <p>{{talk.description}}
   </div>`
})
class ConfTalks {
  talks = [ {title: 't1', speaker: 'Brian', description: 'talk 1'},
            {title: 't2', speaker: 'Julie', description: 'talk 2'}];
}
@Component({
  selector: 'my-app',
  directives: [ConfTalks],
  template: '<conf-talks></conf-talks>'
})
class App {}
bootstrap(App, [])

The error is

EXCEPTION: Template parse errors:
Can't bind to 'ngForIn' since it isn't a known native property
("<div [ERROR ->]*ngFor="#talk in talks">

解决方案

Since this is at least the third time I've wasted more than 5 min on this problem I figured I'd post the Q & A. I hope it helps someone else down the road... probably me!

I typed in instead of of in the ngFor expression. It should be:

<div *ngFor="#talk of talks">


Note that the ngFor syntax "desugars" into the following:

<template ngFor #talk [ngForOf]="talks">
  <div>...</div>
</template>

If we use in instead, it turns into

<template ngFor #talk [ngForIn]="talks">
  <div>...</div>
</template>

Since ngForIn isn't an attribute directive with an input property of the same name (like ngIf), Angular then tries to see if it is a (known native) property of the template element, and it isn't, hence the error.

这篇关于Angular2例外:无法绑定到'ngForIn',因为它不是一个已知的原生属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆