【问题标题】:loading component before directive in angular2在angular2中的指令之前加载组件
【发布时间】:2016-09-16 07:56:16
【问题描述】:

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

}

【问题讨论】:

  • 请添加代码。

标签: angular loading angular2-directives angular-components


【解决方案1】:

您可以使用ngOnChanges()。当绑定到输入的值发生更改时调用此方法。

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

【讨论】:

    猜你喜欢
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多