#5 ✏️Add Editor
Add Forms module
We will add the FormsModule
in 📝src/app/app.module.ts
as shown below.
Creating the data model
Create new a folder src/app/models
. Create a new file 📝src/app/models/post.ts
and paste the following code
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 📁services
and 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.
Inject the AngularFirestore
in the constructor.
Now we will add the method to create a new post. The method to add a new blog post is shown at https://github.com/AnkitSharma-007/blogsite/blob/master/src/app/services/blog.service.ts#L14-L17. Put this method in blog.service.ts
file.
Install CkEditor package
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
BlogEditorComponent
Open 📝src/app/components/blog-editor/blog-editor.component.ts
and add the import definitions:
We will also initialize some properties as shown at https://github.com/AnkitSharma-007/blogsite/blob/master/src/app/components/blog-editor/blog-editor.component.ts#L16-L20
We will create a method to define the configuration for the blog editor. You can get the method definition from https://github.com/AnkitSharma-007/blogsite/blob/master/src/app/components/blog-editor/blog-editor.component.ts#L50-L66
We will invoke this method inside ngOnInit
as shown below.
Update the BlogEditorComponent template
Open src/app/components/blog-editor/blog-editor.component.html
and put the code as shown at https://github.com/AnkitSharma-007/blogsite/blob/master/src/app/components/blog-editor/blog-editor.component.html
Add a new blog
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.
Last updated
Was this helpful?