Try to search your question here, if you can't find : Ask Any Question Now ?

RXJS Subject's value not updated on reloading page

HomeCategory: stackoverflowRXJS Subject's value not updated on reloading page
Avatarpragati asked 4 months ago

I have created a service which has a subject named selectedNode. I am trying to update the value of subject when I gives direct URL for loading a page. The issue is that the value of Subject is not updated when I tries to update the value directly when passing the URL.

e.g. if I opens URL localhost:4200/blog/page/subpage it should update selectedNode with value page/subpage. But, it does not.

My code is as below:

blog.component.ts

import {Component, OnInit} from '@angular/core';
import {BlogService} from '../../services/blog.service';
import {Router} from '@angular/router';

@Component({
  selector: 'app-blog',
  templateUrl: './blog.component.html',
  styleUrls: ['./blog.component.css']
})
export class BlogComponent implements OnInit {

  constructor(private blogService: BlogService, private router: Router) {
  }

  ngOnInit() {
    let routerURL = this.router.url;
    routerURL = routerURL.replace('/blog/', '');
    console.log(routerURL);
    this.blogService.selectedNode.next(routerURL);
  }
}

Here the below code does not update the SelectedNode Subject. I checked while debug. The value is not updated in Subject despite of routerURL has a different value and the this.blogService.selectedNode.next(routerURL); is also called.

my blog.service.ts file is as follows:

import {Injectable} from '@angular/core';
import {Subject} from 'rxjs';
import {HttpClient} from '@angular/common/http';

@Injectable()
export class BlogService {

  public selectedNode: Subject<string> = new Subject<string>();
  public sideNavDisabled: Subject<boolean> = new Subject<boolean>();

  constructor(private http: HttpClient) {
  }

  public getTreeNodes() {
    return this.http.get('/assets/tree.json');
  }

  public getPageContent(page: string) {
    return this.http.get('/assets/TreeStructure/' + page + '.html');
  }
}

My complete code is present at https://github.com/vibhorgoyal18/atest-blog
and can be viewed on https://stackblitz.com/github/vibhorgoyal18/atest-blog

1 Answers
Best Answer
AvatarMatthias answered 4 months ago
Your Answer

0 + 17 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com