我正在尝试将父组件中的值传递给我试过的子组件:
这是我的父组件:
import {Component, View, NgFor} from 'angular2/angular2';
import {DisplayCard} from '../display-card/display-card';
@Component({
selector: 'display'
})
@View({
templateUrl: './components/display/display.html',
directives: [DisplayCard, NgFor]
})
export class Display {
displays: Array<any>;
constructor(){
this.displays = [
{name: "Lobby", appName: "Animal App", Source: 3},
{name: "Wall", appName: "Idk App", Source: 3},
{name: "Other Wall", appName: "Monkey App", Source: 3},
{name: "Lobby Kiosk", appName: "Car App", Source: 3},
{name: "Another Brick in the Wall", appName: "Pink Floyd App", Source: 3},
]
}
}
父组件HTML:
<div class="col-md-3" *ng-for="#display of displays">
<display-card name="{{display.name}}"></display-card>
</div>
子组件:
import {Component, View} from 'angular2/angular2';
@Component({
selector: 'display-card'
})
@View({
templateUrl: './components/display-card/display-card.html'
})
export class DisplayCard{
name: string;
}
儿童HTML:
<div class="display-card">
<div class="display-card-header">
</div>
<div class="display-card-body">
<h1 class="display-card-title">{{name}}</h1>
<h4 class="display-card-subtitle"></h4>
</div>
</div>
我将name
属性添加到子组件中,因为这是我在尝试时遇到的错误:
EXCEPTION: Can't bind to 'name' since it isn't a known property of the '<display-card>' element and there are no matching directives with a corresponding property
组件是否可以实现这一点?
答案 0 :(得分:3)
您需要在组件声明中指定properties
:
@Component({
selector: 'display-card',
properties: ['name']
})