Angular Content Dictionary Service
How to build a super simple Content / Dictionary Service in Angular? Check the blogpost on the platform you like:
If you found this valuable, please support me by sharing the blogpost. Want to support me with
This website collects cookies to deliver better user experience
If you need a content service with multiple languages, I highly recommend NGX-Translate. This package offers a simple API and Pipe mechanism to re-use pieces of content but also supports multiple languages.
ng generate service services/content
app.module.ts
file. If your project has different settings for the CLI, it could appear in another Angular Module.import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ContentService {
constructor() {}
}
dictionary
folder where I make the general.dictionary.json
file, but I can have more dictionary files there.{
"pages" : {
"home": {
"title" : "Home",
"name" : "Company name"
}
}
}
cache$
where we make a BehaviourSubject
. We do this because we can subscribe to this BehaviourSubject
from any place in the application. And the best part is, when a content item is being updated, it will automatically be updated everywhere.private cache$: BehaviorSubject<any> = new BehaviorSubject(null);
If you wonder what the difference is between different Subjects, please check this post "When Use RxJS Subject, BehaviourSubject, ReplaySubject, AsyncSubject, or Void Subject in Angular"
import content from '../dictionary/general.dictionary.json';
BehaviourSubject
is null
, we need to add the content from the dictionary file.constructor() {
if (this.cache$.getValue() === null) {
this.cache$.next(content);
}
}
BehaviourSubject
with its content to the subscribers. We do that by returning the cache$
property. The return type of the method is any
for this case because you don't have to type the structure of your dictionary. But if you want to, you can do it.public content(): BehaviorSubject<any> {
return this.cache$;
}
content()
method with the .getValue()
method chained to it.public value(): any {
return this.content()?.getValue();
}
import { Injectable } from '@angular/core'
import { BehaviorSubject } from 'rxjs'
import content from '../dictionary/general.dictionary.json'
@Injectable({
providedIn: 'root'
})
export class ContentService {
private cache$: BehaviorSubject<any> = new BehaviorSubject(null)
constructor() {
if (this.cache$.getValue() === null) {
this.cache$.next(content)
}
}
public content(): BehaviorSubject<any> {
return this.cache$
}
public value(): any {
return this.content()?.getValue()
}
}
HomepageComponent
.ng generate component components/homepage
ContentService
into our Component and expose it via the constructor
.import { Component } from '@angular/core';
@Component({
selector: 'homepage',
templateUrl: './homepage.component.html',
styleUrls: ['./homepage.component.scss']
})
export class HomepageComponent {
constructor(private contentService: ContentService) {}
}
ContentService
to the HTML template. We create a content
property in our Component and add the value via the constructor
.import { Component } from '@angular/core';
@Component({
selector: 'homepage',
templateUrl: './homepage.component.html',
styleUrls: ['./homepage.component.scss']
})
export class HomepageComponent {
public content: any = null
constructor(private contentService: ContentService) {
this.content = this.contentService.value()
console.log('content:', content)
}
}
console.log
, you can test if everything works as expected.ContentService
.<header>
<h1>{{content?.pages?.home?.title ?? 'title'}}</h1>
</header>
How to build a super simple Content / Dictionary Service in Angular? Check the blogpost on the platform you like:
If you found this valuable, please support me by sharing the blogpost. Want to support me with
Did you know that you can create a Developer blog like this one, yourself? It's entirely for free. 👍💰🎉🥳🔥