2016-09-16 8 views
1

angular2 응용 프로그램에서 작은 구성 요소를 만들었습니다. 지시어로 사용하여 다른 응용 프로그램에서 작은 뷰로 들어갑니다. 이 지시어를 사용하고있는 구성 요소의 옵션 만 정의합니다. 이 지시자는 항상 옵션을 가질 것으로 예상하고 나의 경우에는 서버로부터 데이터를 가져올 때까지 'someOptions'가 정의 될 때까지 시간이 걸립니다. 그러나이 '예제'구성 요소는 즉시이를 기대하며 어떻게 연기 할 수 있습니까? 이 지시어를 계속 사용하면서이 지침을 사용하는 구성 요소에 옵션을 정의한 후에는 각도 만 활성화 할 수 있다는 것을 알고 있습니까?angular2에서 지시문 앞에 로딩 구성 요소

// the directive Im planning to use in differnt views 
@Component({ 
    selector: 'expml-comp' 
}) 

export class Example1 { 
@Input() options: any; 
    constructor() {} 
    ngOninit(){ 
    //puting data from options to directive 
    } 
} 

//and the component where I want to use this directive: 
@Component({ 
    template: '<expml-comp [options] = 'definedData'></expml-comp>', 
}) 
export class Example2 { 
    constructor() { } 
    private definedData:any; 
//here I need to define the options but before the data comes from server  //the Example1 dir is already activated 
//and it didnt find definedData so it breaks 
ngOnInit(){ 
//this.definedData=... 
} 

} 
+0

코드를 추가하십시오. –

답변

0

당신은 ngOnChanges()를 사용할 수 있습니다. 이 메소드는 입력에 연결된 값이 변경 될 때 호출됩니다.

export class Example1 { 
    @Input() options: any; 
    constructor() {} 
    ngOnChanges(changes){ 
    if(this.options != null) { 
     // options are set 
    } 
    } 
} 
관련 문제