如何使用ngOninit中的事件发射器调用共享服务?

时间:2018-10-03 07:03:13

标签: angular angular4-router

               I have a leftPanel where I have a list of products.
    I want to display the data on click of product on the right panel 
    which has two child components rightPaneldetails and rightPanelGraph.
    When I am clicking the product only the html are getting displayed and not the data.
 RouterModule.forRoot([
                    { path:'details/:id', component:RightPanelDetailsComponent},
                    { path:'graph/:id', component: RightPanelGraphComponent}
                ]), 

               I want to call a shared service like 


         export class SharedService {
                selectedBusinessUnitS = new EventEmitter<Array<Object>>();
                selectedBusinessObjet = new EventEmitter<Array<Object>>()
            }
                I need to call the function getUpdatedBOs on ngOninit page load
            @RightPanelComponent
                ngOnInit() {
                    console.log("right panel details loading");
                    this.route.params.subscribe(params => {
                      this.id = params['id'];
                      console.log(this.id);
                    });
                    this.getUpdatedBOs();
                  }
                getUpdatedBOs() {
                        this._sharedService.selectedBusinessObjet.subscribe((data: Object) => {
                            this.selectedObjectDetails = data;
                }  
 @leftPanelComponent
          onBuisenessUnit(businessUnit) {
                this.resetData(businessUnit);
                this.clickFlag = 1;
                let temp: Array<object> = this.ibusinessUnits;
                temp['clickFlag'] = (this.isAnyBuSelected == false) ? undefined : this.clickFlag;
                temp['isAnyBuSelected'] = this.isAnyBuSelected;
                this.keyObjMap.clear();
                this.names = [];
                this.ibusinessUnits.forEach(ibo => {
                    if (ibo['isBUActive'] || !this.isAnyBuSelected) {
                        this.buObjectsMap.get(ibo['id']).forEach(obj => {
                            this.keyObjMap.set(obj['id'], obj);
                            this.names.push(obj);
                        });
                    }
                });
                this._sharedService.viewSelected.emit(1);
                this._sharedService.selectedBusinessUnitS.emit(temp);
            }
            Calling on left panel using [routerLink]="['details', objName.id]

如何在第一次单击本身时获得页面加载时的共享服务数据?第一次单击产品时如何调用事件发射器共享服务? 第一次单击产品时如何获取页面加载时的共享服务数据?第一次单击产品时如何调用事件发射器共享服务?

1 个答案:

答案 0 :(得分:0)

据我所知,这里存在语法错误:

getUpdatedBOs() {

  this._sharedService.selectedBusinessObjet
    .subscribe((data: Object) => {
      this.selectedObjectDetails = data;
}

您需要添加一个}和一个)

getUpdatedBOs() {

  this._sharedService.selectedBusinessObjet
    .subscribe((data: Object) => {
      this.selectedObjectDetails = data;
    });
}