我在我用作指令的 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=...
}
}