如何使用Angular 2中的属性创建指令?

时间:2016-05-25 05:02:17

标签: angular

我不明白如何使用属性创建组件。你能看出发生了什么事吗?

我不断收到此错误:“无法绑定到'val',因为它不是已知的本机属性”

// app.html

There should be a list of fruit here with a line after each one
<ul>
  <template ngFor let-fruit [ngForOf]="fruits">
    <li>{{fruit}}</li>
    <part [val]="5"></part>
  </template>
</ul>

part.component.ts

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

@Component({
  selector: 'part',
  templateUrl: 'app/part.html'
})
export class PartComponent { 
  // meaningless for now. I just want this thing 
  // to take a parameter
  val: number;
}

plunk

如果我可以被允许建议,Angular团队提供的一个演示了该技术最常见的功能(如these,但是以plunk形式,甚至更基本的话)对我来说会更有用比the documentation。如果您知道这样的事情,我会非常感谢转介。

2 个答案:

答案 0 :(得分:2)

缺少@Input()装饰器

export class PartComponent { 
  // meaningless for now. I just want this thing 
  // to take a parameter
  @Input() val: number;
}

答案 1 :(得分:2)

您需要在主要组件中声明子指令。将directives:[PartComponent]添加到您的AppComponent元数据中。 @Input()属性需要val

相关问题