1

I am working with Angular 4

I have a core component which is the parent component(menu, header, footer... included in core component) as below

CoreComponentHTML

<div class="wrapper" [ngClass] = "{'confirmation-body' : (showConfirm == true)}">   
<app-header></app-header>
  <app-sidemenu></app-sidemenu>
  <div class="content-wrapper">    
    <app-breadcrumb> </app-breadcrumb>
  <router-outlet></router-outlet>
  </div>
</div>
  <footer class="main-footer">
    Copyright &copy; 2017 <strong><a href="">XXXXXXXXXXXXXX Solutions</a>.</strong> All rights reserved.
  </footer>

CoreComponent.ts

import { Component, OnInit,Injectable,AfterContentInit ,AfterViewInit, ViewEncapsulation, OnDestroy} from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Event as RouterEvent,
  NavigationStart,
  NavigationEnd,
  NavigationCancel,
  NavigationError} from '@angular/router';
import {MenuService} from '../../shared/services/menu.service';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/toPromise';
import { ConfirmationSharedService } from 'app/sharecomponents/services/confirmation-shared.service';

@Component({
  selector: 'app-core',
  templateUrl: '../views/core.html'
})

export class CoreComponent implements OnInit, OnDestroy {
  public userPin: any;
  showConfirm: boolean;
  alive: boolean = false;
  constructor (private confirmationService: ConfirmationSharedService) {}

  setUserPin (responce) {
    console.log(responce, 'core pin core pin ')

    if (responce !== 0) {
      this.userPin = responce;
     }

  }

  ngOnInit() {
    this.confirmationService.getShowConfirm()
    .subscribe(suc => {
      console.log('1111111111111', suc);
      this.showConfirm = suc});

  }

  ngOnDestroy() {

  }
}

I have a LanguagesViewComponent in one module(xxxxxxxxxxxxx module), ConfirmationSharedService in another module(this module is imported in CoreModule )

i am setting a value to BehaviourSubject from LanguagesViewComponent which is declared in ConfirmationSharedService as below

this.confirmService.setShowConfirm(true);

BehaviourSubject is declared as below in ConfirmationSharedService

showConfirm: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);

    setShowConfirm(data) {
        this.showConfirm.next(data);
    }

    getShowConfirm(): Observable<boolean> {
        return this.showConfirm.asObservable();
    }

the problem is when i set value to showConfirm in LanguagesViewComponent that new value is getting in CoreComponent(subscribed in ngOnInit() in CoreComponent)

Santhosh
  • 1,053
  • 1
  • 20
  • 45
  • isn't that what a behavior subject is supposed to do , when ever it is updated it will notify its observable to subscribe you might need to close the subscription in the ngOnInit – Rahul Singh Mar 29 '18 at 07:08
  • @RahulSingh, I am not unsubscribing that at all, please see the updated issue – Santhosh Mar 29 '18 at 07:11
  • I don't get the issue. You want the value to be received in CoreComponent or not? – Akanksha Gaur Mar 29 '18 at 08:21
  • If you don't want to receive the value when the component is not active int the current view, then you have to unsubscribe from the behaviorsubject, you can do it in `ngOnDestroy`, Check [this](https://stackoverflow.com/a/41177163/2526437) answer for a how-to – cyberpirate92 Mar 29 '18 at 09:41

0 Answers0