I try to change the page title with BrowserModule. I added the BrowserModule and Title in application module, like here: https://angular.io/guide/set-document-title
In a child module (I tried to add here the service and module (BrowserModule) too) I have a component where I insert the Title service, but that service is 'undefined'.
module
import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { RouterModule, Routes } from "@angular/router";
import { ProductService } from "../../services/Product";
import { ProductComponent } from "../../components/product/component";
import { ProductResolve } from "../../components/product/resolve";
const routes: Routes =
[
{
path: "produs/:url",
component: ProductComponent,
resolve:
{
Product: ProductResolve,
},
},
];
@NgModule({
imports:
[
CommonModule,
RouterModule.forChild(routes),
],
providers:
[
ProductResolve,
ProductService,
],
declarations:
[
ProductComponent,
],
exports:
[
RouterModule,
ProductComponent,
],
})
export class ProductModule { }
component:
import { Component, OnInit, OnDestroy } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { Product } from "../../services/Product";
import { Title } from '@angular/platform-browser';
@Component({
templateUrl: "../../templates/product/component.html",
styleUrls: [
"../../sass/product/component.scss",
]
})
export class ProductComponent implements OnInit, OnDestroy
{
private Subscribe: any;
constructor(private titleService: Title, private Route: ActivatedRoute)
{
}
ngOnInit()
{
this.Subscribe = this.Route.data.subscribe(this.process);
}
private process(product: Product)
{
//console.log(this.title);
//this.titleService.setTitle(product.Title);
}
ngOnDestroy()
{
this.Subscribe.unsubscribe();
}
}
app module
import { BrowserModule, Title } from '@angular/platform-browser';
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
...
@NgModule({
declarations: [
...
],
imports: [
...
BrowserModule,
],
providers: [LoadingScreenService, Title],
bootstrap: [AppComponent],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class AppModule { }
Seems like you are losing the scope.
In the process()
scope this
doesn't represent your class instance anymore.
Adding .bind(this)
should solve your problem.
this.Subscribe = this.Route.data.subscribe(this.process.bind(this));