[英]Angular HTTP Observable @Input
here is my pseudo code: 这是我的伪代码:
service 服务
getData(): Observable<MyData[]>{
return this.http.get<Data[]>(`https://localhost/api/data`);
}
component: 零件:
myData: Data[];
[...]
ngOnInit(){
this.myService.getData.subscribe( data => {
this.myData = data;
console.log(data);
});
}
Template: 模板:
<app-subComponent *ngIf="data" class="cat-row" [data]='{"type": "row", "data": myData}'></app-subComponent>
SubComponent: 子:
@Input() data: any;
ngOnInit(){
console.log(data);
}
My Problem: In this scenario, console.log gives me an empty array. 我的问题:在这种情况下,console.log给了我一个空数组。 If I don't pass this data attribute to my subcomponent via input, my first console.log gives me the correct array.
如果我没有通过输入将此数据属性传递给我的子组件,我的第一个console.log为我提供了正确的数组。
So maybe i miss something if i want to use http, observable, templating and @input in one "row" ? 所以,如果我想在一个“行”中使用http,observable,templating和@input,也许我会想念一些东西?
Thanks for your help! 谢谢你的帮助!
You should implement OnChanges then you can able to detect input when it changes. 您应该实现OnChanges,然后您可以在更改时检测输入。
Import package 导入包
import {Component, Input, OnChanges, SimpleChanges }
Then implements Onchanges 然后实现Onchanges
export class SubComponent implements OnChanges { @Input() data: any; constructor() { } ngOnChanges(changes: SimpleChanges){ if(changes["data"] && this.data){ // here you get the result } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.