Todo List Tutorial
Primary version
Primary version
  • Welcome to the ngGirls tutorial!
  • Workshop: Todo List
    • 👀About
    • #0: 💃 Introduction
    • #1: ⌛ Installations
      • a. StackBlitz instructions
    • #2: 🅰 Angular kicks in
    • #3: 📐 Component
    • #4: ✏ A new component
      • a. StackBlitz instructions
    • #5: 💼 Class
    • #6: 📥 Property binding
    • #7: 📤Event binding
    • #8: 📎 Element ref - #
      • a. About
      • b. Explore
      • c. Resources
    • #9: 📋 The To Do list
    • #10: ➕ New component: todo-item
    • #11: ⛓ Interface
      • a. StackBlitz instructions
    • #12: 📌Add items
    • #13: 🚧 Refactor App Component
    • #14: 💅 Adding Style
    • #15: 🔋 Creating a Service
    • #16: 🎁 Add Items Using the Service
    • #17: 💾Local storage
    • #18: 🗑 Remove item
    • #19: 🔘 Adding a checkbox
    • #21: 💪 Enrich the todo-item component
    • Appendix 1: Git and GitHub
    • Appendix 2: 🛰 Deploying your app
      • Deploy to Azure Static Web Apps
      • Deploy to GitHub Pages
        • a. StackBlitz instructions
    • Appendix 3: Tutorial Extensions
    • Appendix 4: Generating a new project
    • Troubleshooting
      • Installation
  • More Workshops
    • Second workshops - CRUD and HTTP
      • #1 MongoDB
      • #2 Local server
      • #3 http in diagrams
      • #4 POST
      • #5 GET
      • #6 DELETE and PUT
    • Gallery with Rx.js
      • #0: Init photo gallery
      • #1: Observable
      • #2: More interaction! Upload photos
      • #3: Merging Observables
      • #4: Filtering by category
      • #5: Adding photos to category
    • Forms
      • #1: Template-driven forms
      • #2: Reactive forms
      • #3: Form builder
    • NgRx
      • #1: Actions and reducers
      • #2: Store Devtools
      • #3: Implementing actions in app
      • #4: Selectors
    • RxJS
      • Operators
      • Play time!
    • Blog Editor
      • #1 ⚙️Configuring firebase
      • #2 💅Add Angular Material
      • #3 💅Add Bootstrap
      • #4 🧭Add Navigation
      • #5 ✏️Add Editor
      • #6 ⚙️Connect Database
      • #6 💅Add Feed
      • #7 ✏️Edit Post
      • #9 🚀Deploy
      • #10 ✏ ️Next Steps
Powered by GitBook
On this page
  • Create custom pipes
  • Get the blogs from database
  1. More Workshops
  2. Blog Editor

#6 ⚙️Connect Database

Create custom pipes

We will add two custom pipes in our app

  • Excerpt: which will show a summary of post in blog card.

  • Slug: which will show the URL slug for a post.

We will create a a new pipe named ExcerptPipe. Right-click on the folder 📁custompipes and then navigate to 'Angular Generator', select 'Pipe' and provide the name ‘excerpt.

Replace the transform method in the file 📝 src/app/custompipes/excerpt.pipe.ts with the following code :

transform(content: string) {
    const postSummary = content.replace(/(<([^>]+)>)/ig, '');
    if (postSummary.length > 300) {
    return postSummary.substr(0, 300) + ' [...]';
    } else {
    return postSummary;
    }
}

Furthermor we create a slug pipe. Right-click on the folder 📁custompipes and then navigate to 'Angular Generator', select 'Pipe' and provide the name ‘slug’.

Replace the transform method in the file 📝 src/app/custompipes/slug.pipe.ts with the following code:

transform(title: string) {
    const urlSlug = title.trim().toLowerCase().replace(/ /g, '-');
    return urlSlug;
}

Get the blogs from database

getAllPosts(): Observable<any> {
    const blogs = this.db.collection('blogs', ref => ref.orderBy('createdDate', 'desc')).snapshotChanges().pipe(
    map(actions => {
        return actions.map(
        c => ({
            postId: c.payload.doc.id,
            ...c.payload.doc.data()
        }));
    }));
    return blogs;
}
Previous#5 ✏️Add EditorNext#6 💅Add Feed

Last updated 5 years ago

We will add the getAllPosts method in 📝src/app/services/blog.service.ts file to fetch all the blog posts from database. The definition for getAllPosts is shown at

blog.service.ts (Github)