I have a side navigation bar and a content div. What I currently want to achieve is that whenever I click on any element in side nav. The innerText of that nav item should be displayed in the content div. My code is as below
sidenav.component.ts
import {Component} from '@angular/core';
import {FlatTreeControl} from '@angular/cdk/tree';
import {MatTreeFlatDataSource, MatTreeFlattener} from '@angular/material';
import {BlogService} from '../../../../services/blog.service';
interface FoodNode {
name: string;
children?: FoodNode[];
}
const TREE_DATA: FoodNode[] = [
{
name: 'Fruit',
children: [
{name: 'Apple'},
{name: 'Banana'},
{name: 'Fruit loops'},
]
}, {
name: 'Vegetables',
children: [
{
name: 'Green',
children: [
{name: 'Broccoli'},
{name: 'Brussel sprouts'},
]
}, {
name: 'Orange',
children: [
{name: 'Pumpkins'},
{name: 'Carrots'},
]
},
]
},
];
interface ExampleFlatNode {
expandable: boolean;
name: string;
level: number;
}
@Component({
selector: 'app-sidenav',
templateUrl: './sidenav.component.html',
styleUrls: ['./sidenav.component.css'],
providers: [BlogService]
})
export class SidenavComponent {
treeControl = new FlatTreeControl<ExampleFlatNode>(
node => node.level, node => node.expandable);
private transformer = (node: FoodNode, level: number) => {
return {
expandable: !!node.children && node.children.length > 0,
name: node.name,
level,
};
}
treeFlattener = new MatTreeFlattener(
this.transformer, node => node.level, node => node.expandable, node => node.children);
dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);
constructor(private blogService: BlogService) {
this.dataSource.data = TREE_DATA;
}
hasChild = (_: number, node: ExampleFlatNode) => node.expandable;
routeToPage(event: any) {
this.blogService.changeSelectedNode(event.target.innerText);
}
}
content.component.ts
import {Component, OnInit} from '@angular/core';
import {BlogService} from '../../../../services/blog.service';
@Component({
selector: 'app-content',
templateUrl: './content.component.html',
styleUrls: ['./content.component.css'],
providers: [BlogService]
})
export class ContentComponent implements OnInit {
page: string;
constructor(private blogService: BlogService) {
this.page = this.blogService.selectedNodeName;
console.log(this.page);
}
ngOnInit() {
}
}
blog.service.ts
import {Injectable} from '@angular/core';
import {Subject} from 'rxjs';
@Injectable()
export class BlogService {
public selectedNodeName: string;
public sideNavToggle: boolean;
public selectedNode: Subject<string> = new Subject<string>();
public sideNavDisabled: Subject<boolean> = new Subject<boolean>();
constructor() {
this.selectedNode.subscribe((node) => {
this.selectedNodeName = node;
});
}
changeSelectedNode(node: string) {
this.selectedNode.next(
this.selectedNodeName = node
);
}
}
Please help me resolving this. I am successfully able to pass content to service but the code in content ts file is not getting executed each time I clicks on side nav item.
My complete code can be found here - https://stackblitz.com/github/vibhorgoyal18/atest-blog
It took me a while, but here is your example updated and working: Demo
This is what I changed:
First your service was defined incorrect. You added your service like [providers: BlogService] in your SideNavComponent and ContentComponent. Those components then didn't get the same instance of BlogService and therefore it didn't work. You have to place the providers: [BlogService] into the AppModule.
Second: I removed the section where you the subscribe in your service and moved it to the ContentComponent. Otherwise Angular does not bind correctly to that variable.
EDIT: If you are using Angular 6+ you can also add this in your service and remove it from providers in your module:
@Injectable({
providedIn: 'root'
})
This provides the service in all of your app. If you only want the service to be provided in one module, you can specify that module insteed of root.