Create new a folder src/app/models. Create a new file 📝src/app/models/post.ts and paste the following code
export class Post {
postId: string;
title: string;
content: string;
createdDate: any;
constructor() {
this.content = '';
}
}
Create the blog service
We will create a service to handle our database operations. Create a new service by making a right-click on the folder 📁servicesand navigating to 'Angular Generator','Component' and provide the name ‘blog’.
Open the 📝src/app/services/blog.service.ts file and add the following import definitions.
We will use CKEditor for adding and editing our blog post. CKEditor is a Smart WYSIWYG(What you see is what you get) editor which provides us with great editing capabilities.
⏰We already installed into stackblitz @ckeditor/ckeditor5-angular @ckeditor/ckeditor5-build-classic
Imports the CKEditorModule in 📝src/app/app.module.ts as shown below.
Add the blog editor
We will create a new component for adding and editing the blog. Let’s make a right-click on the folder 📁components. Navigate to 'Angular Generator', select 'Component' and provide the name ‘blog-editor’.
Add a route to the addpost page
Add the route for this component in 📝app.module.ts as shown below:
Add CKEditor to BlogEditorComponent
Open 📝src/app/components/blog-editor/blog-editor.component.ts and add the import definitions:
We will now implement the feature of adding a new blog to our application. Open 📝src/app/components/blog-editor.component.ts and inject the following service definitions in the constructor.
Add the following code in @Component decorator DatePipe can be injected.
We will create a new method called saveBlogPost which is used to add a new post to our database. The definition for this method is shown below.
This method will be invoked on click of Save button. We will add the following code definition for Cancel button.
Add buttons in Nav bar
We will add the navigation button to blog editor and home page in the nav bar. Add the following code inside the <mat-toolbar> in the file 📝src/app/components/nav-bar/nav-bar.component.html:
Add BlogEditorComponent styles
We will add styling for blog editor in 📝styles.scss file with the following code:
Test it out
Open the browser and click on “AddPost” button on the nav-bar. You will be navigated to the blog editor page. Add a new blog and click on save button to save the blog in thee database. Open the firebase console, navigate to your project overview page and click on “Database” link in the menu on the left. Here you can see the record for your newly added blog.
import { AngularFirestore } from '@angular/fire/firestore';
import { Post } from '../models/post';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import { Post } from 'src/app/models/post';
import { DatePipe } from '@angular/common';
import { BlogService } from 'src/app/services/blog.service';
import { Router, ActivatedRoute } from '@angular/router';
public Editor = ClassicEditor;
ckeConfig: any;
postData = new Post();
formTitle = 'Add';
postId = '';