1

アプリケーションのさまざまな部分で小さなビューを入力するために、ディレクティブとして使用している angular2 アプリケーションで小さなコンポーネントを作成しました。このディレクティブを使用しているコンポーネントでのみオプションを定義します。このディレクティブは常にオプションを持つことを想定しています。私の場合、サーバーからデータを取得するまでに時間がかかり、それから「someOptions」が定義されます。しかし、この「サンプル」コンポーネントはすぐにそれを期待していますが、どうすれば延期できますか? これをディレクティブとして使用し続ける方法を知っていますが、このディレクティブを使用しているコンポーネントでオプションが定義されている場合にのみアクティブにするように angular に指示しますか?

// 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=...
 }

}
4

1 に答える 1

0

使用できますngOnChanges()。このメソッドは、入力にバインドされた値が変更されたときに呼び出されます。

export class Example1 {
  @Input() options: any;
  constructor() {}
  ngOnChanges(changes){
    if(this.options != null) {
      // options are set
    }
  }
}
于 2016-09-16T11:23:05.070 に答える