相关疑难解决方法(0)

根指令上的Angular 2输入参数

示例显示如何在子组件上使用@Input()批注.我的问题是你如何在根组件上使用它?例如,如果您修改上面链接上的代码:

@Component({
selector: 'app',
template: `
    <bank-account bank-name="RBC" account-id="4747"></bank-account>
`,
directives: [BankAccount]
})
class App {
    @Input() something: string;
}

bootstrap(App);
Run Code Online (Sandbox Code Playgroud)

并在HTML中:

<app something="Test"></app>
Run Code Online (Sandbox Code Playgroud)

上面的示例永远不会更新App组件上的某些属性.

typescript angular2-template angular

14
推荐指数
2
解决办法
1万
查看次数

使用ngOnInit()而不是构造函数; @Input()未定义

首先,如果这是一个可怕的设计,我很乐意改变.

在我index.html的身体中,有:

<month [year]="2016" [monthOfYear]="4">Loading...</month>
Run Code Online (Sandbox Code Playgroud)

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

...... ngOnInit()并被正确地称呼.然而,在调用(或许永远,但我不知道如何来确定),两者的时间yearmonthOfYearundefined …

typescript angular2-template angular2-components angular

2
推荐指数
1
解决办法
1314
查看次数