需要帮助!
我有一个组件'form.component.ts'。
------------- form.component.ts -------------
@Component({
selector: 'my-app',
providers: [ShopService],
templateUrl: 'app/admin/shop/shop-form/form.html'
})
export class FormComponent implements OnInit {
private shop:Shop = new Shop();
...
this.route.params.subscribe(params => {
if (params['id'] !== undefined) {
this.shopService.getById(params['id']).subscribe(result => this.shop = result);
}
});
}
我的'form.html'有一个指令,它有一个变量(shop.state)依赖于ajax中加载的数据。
------------- form.html -------------
<state-city [state]='shop.state' (stateSelected)="shop.state = $event" [city]='shop.city' (cidadeSelected)="shop.city = $event"></state-city>
我的问题是,有时ajax请求需要很长时间才能加载,而我的指令是未定义的值。
我需要在请求ajax之后加载指令的东西。
** 已编辑 **
我减少了我的代码以便更好地查看。
总结...我需要首先发生请求ajax然后打印模板。我的<state [state]='shop'></state>
需要此变量才能正常工作。
我的form.component是:
@Component({
selector: 'my-app',
providers: [ShopService],
template: `<state [state]='shop'></state>`
})
export class FormComponent {
private shop: string;
constructor(private shopService:ShopService) {
this.route.params.subscribe(params => {
this.shopService.getById(params['id']).subscribe(result => this.shop = result);
});
}
}
** 已解决 **
问题已解决!!!我使用角度2的方法ngOnChanges解决了。 每个状态更改此方法都被调用。也就是说,在完成ajax调用之后,调用此方法。因此,我们更新变量,我的指令运作良好。
答案 0 :(得分:1)
使用可以使用 ngIf
等待您获取数据,
<state-city *ngIf="shop"
[state]='shop.state'
(stateSelected)="shop.state = $event"
[city]='shop.city'
(cidadeSelected)="shop.city = $event">
</state-city>