【发布时间】:2016-09-22 08:29:18
【问题描述】:
首先,如果这是一个糟糕的设计,我很乐意做出改变。
在我的index.html我体内,有:
<month [year]="2016" [monthOfYear]="4">Loading...</month>
month.component.ts 则为:
import { Component, Input, OnInit } from '@angular/core';
import { DayComponent } from '../day/day.component';
import * as Models from '../../../models/_models';
@Component({
selector: 'month',
moduleId: module.id,
templateUrl: 'month.component.html',
styleUrls: ['month.component.css'],
directives: [DayComponent]
})
export class MonthComponent {
name: string;
days: Models.Day[];
@Input('year') year: number;
@Input('monthOfYear') monthOfYear: number;
month: Models.Month;
ngOnInit() {
this.month = new Models.Month(this.year, this.monthOfYear);
this.name = this.month.getMonthName();
this.days = this.month.days;
}
}
...并且ngOnInit() 被正确调用。但是在打电话时(或者可能永远,但我不知道如何确定)year 和 monthOfYear 都是 undefined。
如何确保在ngOnInit() 中传递值,或者是否有更好的模式来实现这一点?
【问题讨论】:
标签: typescript angular angular2-template angular2-components