@Input()值总是从typescript中未定义

时间:2017-02-23 16:34:02

标签: javascript angular typescript

import {Component, OnInit, ChangeDetectionStrategy, ElementRef, Input} from '@angular/core';
import {FadeInTop} from "../../../../../shared/animations/fade-in-top.decorator";


declare var $: any;
@FadeInTop()
@Component({
    selector: 'order-progressbar-dynamic',
    templateUrl: 'order-progressbar-dynamic.component.html',
})
export class OrderProgressBarDynamicComponent {


    @Input() public data: any;
    public progressBar: {index: number, btnID: string, btnTag: string, title: string, status: boolean, statusTag: string, prevStepStatus: boolean}[];


    constructor(private el: ElementRef) {
        this.generateDataForProgressbar();
    }

    public generateDataForProgressbar() {
        if (this.data) {
            this.progressBar = [
                {
                    "index": 0,
                    "btnID": "orderConfirmed",                                        
                    "status": this.data.orderStatus.saleConfirmed,                    
                },
                {
                    "index": 1,
                    "btnID": "item",
                    "status": this.data.orderStatus.item,

                }                
            ]
        }
    }

}

如果(this.data)一直失败,数据是未定义的,但是当从order-progressbar-dynamic.component.html使用时,我确实得到了值。如何使@Input值从类方法中可用?

1 个答案:

答案 0 :(得分:3)

使用ngOnInit挂钩获取@Input

constructor(private el: ElementRef) {}

ngOnInit() {
  this.generateDataForProgressbar();
}