It seems like everything is fetched correctly, but i still cannot render result of http request in view. Here goes.
This is my category service :
@Injectable()
export class CategoryService {
private _category : BehaviorSubject<any> = new BehaviorSubject([]);
public category : Observable<Category> = this._category.asObservable();
constructor(private myHttpService : HttpService){ }
public getCategory(id : String) : void {
let params = new URLSearchParams();
params.append('id', `${id}`);
this.myHttpService.get(Constants.catalog+"/category",params)
.map(res =>res.json())
.subscribe(categoryJson => {
const category = Category.fromJson(categoryJson);
console.log(category) //<- this prints category object correctly
//update BehaviorSubject
this._category.next(category);
});
}
}
This is my category component :
export class CategoryComponent implements OnInit {
private category : Observable<Category>;
constructor(private categoryService : CategoryService) {
this.category = categoryService.category;
console.log(this.category)// <-- this prints Observable { _isScalar=false, source=BehaviorSubject, lift=function(), more...}
//when i drill down i can find category object under source/value
}
ngOnInit() {
this.categoryService.getCategory('2002');
}
}
This is my category view :
I have experimented here a lot :
1.Wrapped category in array and used for with async pipe
2.Used elvis operator to display category?.name (btw this works for promise, bot not observable)
<div class="ui divided items">
<app-product-list [products]="category?.products"></app-product-list>
</div>
This also doesnt work
<p> {{category?.title}}</p>
This is my product-list component :
export class ProductListComponent implements OnInit {
@Input()
private products : Product[];
constructor() {}
ngOnInit() {
console.log(this.products); // this is always undefined
}
}
This is my view product-list view:
<div class="container">
<app-product
*ngFor="let product of products | async"
[product]="product"
class='item'></app-product>
</div>
And finally this is my Product-component
export class ProductComponent implements OnInit {
@Input()
private product : Product;
constructor() { }
ngOnInit() {}
}
And my Product view :
<p>
product works! {{product?.title}}