2016-09-16 4 views
1

私はangle2アプリケーションで小さなコンポーネントを作成しましたが、これはディレクティブとして使用しています。このディレクティブを使用しているコンポーネントのオプションだけを定義します。 このディレクティブは常にオプションが必要ですが、私の場合はサーバーからデータを取得するまでに時間がかかり、その後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 
    } 
    } 
} 
関連する問題