I have declared a class which has two arrays of objects. The arrays get filled by information from backend. I am using Observable and Http to get information from backend. The global class is declared in provider array of module.ts file, so it is declared at the highest point. In one of the top component I am subscribing to the service to fill the arrays in ngOnInit method. The data array service class in injected in the component's constructor. I see the arrays are getting populated with the right information in this class. But when I inject the same service class in child component, all arrays are null. Why? is there any way to retain the updated values? Please help.
Here is my code:
Global class AdminProductDisplayInfo (this class is added into the provider array in app.module.ts file
// admin-product-display-info.ts
import { ComponentGroupDisplayInfo } from './component-group-display';
import { ServiceCatDisplayInfo } from './service-cat-display-info';
export class AdminProudctDisplayInfo {
public cgc_info_list: ComponentGroupDisplayInfo[] = [];
public service_cat_info_list: ServiceCatDisplayInfo[] = []
}
// Service to get data from backend
import { AdminProudctDisplayInfo } from '../models/admin-product-display-info';
@Injectable()
export class BagProductService {
private backendURL = 'http://localhost:8080/BAG';
constructor (private http: Http) {}
/* This method gets the data necessary to display from backend */
getAdminProductDisplayInfo(): Observable<AdminProudctDisplayInfo> {
let adminURL = this.backendURL.concat('/admin/getAdminProudctDisplayInfo');
let body = JSON.stringify("");
let headers = new Headers({'Content-type': 'application/json'});
let options = new RequestOptions({headers: headers});
return this.http.post(adminURL, body, options)
.map(this.extractData)
.catch(this.handleError);
}
}
// Component from which BagProductService and AdminProductDisplayInfo will be used to subscribe and get the data
import { BagProductService } from '../services/adminBagProduct.service';
import { ServiceCatDisplayInfo } from '../models/service-cat-display-info';
@Component({
selector: 'admin-add-bag-Product',
templateUrl: './admin-add-bag-product.html'
})
export class AdminAddBagProduct implements OnInit {
constructor(private router: Router,
private bagProdService: BagProductService,
private adminProdDispInfo: AdminProudctDisplayInfo) {}
ngOnInit() {
this.bagProdService.getAdminProductDisplayInfo().subscribe(
data => {this.adminProdDispInfo = data; console.log("data received: ", data)},
error => {this.errorMessage = <any>error, console.log("Error Message: ", this.errorMessage)});
}
//Here I see data being populated from backend and everything looks good.
}
// Child component of the above component where data will be used to display but everything is null here.
import { BagProductService } from '../services/adminBagProduct.service';
@Component({
selector: 'admin-add-bag-component',
templateUrl: './admin-add-bag-component.html'
})
export class AdminAddBagComponent {
constructor(private router: Router, private bagProdService: BagProductService, private adminProdDispInfo: AdminProudctDisplayInfo) {
console.log("in Component display: ", this.adminProdDispInfo.cgc_info_list);
// Here this.adminProdDispInfo.cgc_info_list and other array is null.
}
}