Angular 2:组件HTML中的空数组

时间:2017-02-25 12:38:35

标签: angular angular2-services

我正在做一个简单的项目:

有2个输入滑块和一个按钮'计算'当按下按钮时,计算完成并打印到表格中。

我有一点问题,当我按下按钮时,表组件确实显示答案数组为空,但是如果我将它从构造函数打印到控制台,则会显示所有内容。

有人可以帮忙吗? 我想做一个*ngFor并稍后循环遍历这些元素。

服务:

@Injectable()
export class DataService {

  loanArray: Payment[] = [];

  setCalculatedPayments(loanSum: number, loanTime:number) {
    this.loanArray = new PaymentsCalculated(loanSum, loanTime).calculateLoanDetails();
  }

  getCalculatedPayments() {
    return this.loanArray;
  }
}

贷款部分:

export class LoanTablesComponent implements OnInit {

  loanArray: Payment[] = []; // is it always empty?

  constructor(private DataService: DataService) {
    this.loanArray = this.DataService.getCalculatedPayments();
    // here it prints the loanArray as required
    // but if I print out in the .html, it is always zero
  }

  ngOnInit() {
  }

}

计算组件:

export class CalculatorComponent implements OnInit {

  loanSum: number;
  loanTime: number;

  constructor(private DataService: DataService) {
    setTimeout(() => this.loanSum = 500);
    setTimeout(() => this.loanTime = 1);
    // for input sliders
  }

  ngOnInit() {
  }

  calculatePayments(): void{
    this.DataService.setCalculatedPayments(this.loanSum, this.loanTime);
  }

}

感谢您的帮助,只是学习并寻求建议(:

0 个答案:

没有答案