C
C
cb77772020-08-26 10:52:07
JavaScript
cb7777, 2020-08-26 10:52:07

Why is Angular service returning null?

Why is Null being passed to the component?
sidebar item click sidebar.component

onChangeItemAction(item:NavItem){
    console.log('выбранный '+item.name + ' ' + item.id)
    this.shareService.setItemNav(item);
  }

service
item_nav$: BehaviorSubject<NavItem>  = new BehaviorSubject<NavItem>(null);
  
  setItemNav(value:NavItem){
    this.item_nav$.next(value);
    console.log("вставлена в сервис" + value.name)
  }

component where you need to get data table.component
private subscriptions: Subscription[]=[];
  nav_item:NavItem;

  ngOnInit(): void {
    const item = this.shareService.item_nav$.subscribe((data:NavItem)=>{
      this.nav_item = data;
      console.log(this.shareService.item_nav$.getValue());
      console.log(this.nav_item)
    });
    this.subscriptions.push(item);
  }

  ngOnDestroy(): void {
  		this.subscriptions.forEach(s=>s.unsubscribe());
  }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question