【发布时间】:2016-01-21 06:41:27
【问题描述】:
在我的应用程序中,我将 Home 作为根组件,并将另一个名为 list 的通用组件在 Home 中呈现。
我想将数据作为属性传递给来自 XMLHttpRequest 的列表组件。
home.ts
import {Component} from 'angular2/core';
import {DashboardService} from '../../services/dashboard';
import {List} from '../contact/list';
@Component({
selector: 'home',
template:
`
<h3>Home</h3>
<List type="{{type}}"></List>
`
providers: [DashboardService],
directives: [List],
})
export class Home {
private _type: any;
constructor(private _dashboardService: DashboardService) {
this._dashboardService.typeToDisplay()
.subscribe((type) => {
this._type = type;
});
}
}
List.ts
@Component({
selector: 'List',
properties: ['type'],
template: `
<h2>list</h3>
`,
providers: [DashboardService]
})
export class List {
private type: any;
constructor(@Attribute('type') type:string) {
this.type = type;
console.log(type);
}
}
我从 typeToDisplay() 方法获取字符串数据,它是一个 Http 请求并分配给类型变量。但是当我作为属性传递给列表组件时,我在列表构造函数中得到了 null。
我也尝试过,但我得到“类型”字符串的方式相同。
希望我的问题很清楚。
【问题讨论】:
标签: javascript angular angular2-directives