Is there a more efficient method to initialize an inner class within an outer class in Angular 4?
Suppose we have an outer class named ProductsModel that includes ProductsListModel. We need to send the ProductId string array as part of a server-side request. The code below works properly when initializing the inner class inside the outer class.
If not initialized:
export class ProductsModel {
productList: ProductListModel;
}
However, attempting this resulted in the following error message:
Cannot set property ProductId to be undefined.
Therefore, I initialized it as shown below, which functions as intended. Is there a better way to do this initialization?
Outer Class:
export class ProductsModel{
productList = new ProductListModel();
}
export class ProductListModel{
ProductId: string[];
}
-- app.component.ts
export class AppComponent {
// Initialize outer class here:
products = new ProductsModel();
Inside this subscribe block:
DetailsByProductID(){
this.products.productList.ProductId = ['8901', '8902'];
// pass the model object here
this.ProductService.fetchByPID(this.products).subscribe(response => console.log(response));
}
}