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
  • Form builder
  • FormArray
  1. More Workshops
  2. Forms

#3: Form builder

Previous#2: Reactive formsNextNgRx

Last updated 5 years ago

Form builder

Sometimes creating form control instances manually becomes repetitive so we can utilize the help of the FormBuilder.

To use you need to define it in constructor of your class. Let's refactor the existing reactive form.

As you can see in the example link above, with the FormBuilder service you don't need no longer to create a new instance of FormGroup and FormControl each time- it will take care of it for you!

Refactoring should go smoothly, but if you have problems feel free to ask your mentor for help.

FormArray

Imagine that you would like to also add to the form control a way to input skills, but instead of providing long text in one input we want to separate it into different inputs. How do we know how many input controls we need? Well, you can add inputs dynamically by starting with one and adding the next one as we need! This useful ability comes from FormArray

FormArray is an alternative to FormGroup for managing any number of unnamed controls.

Add the skills form controls by updating the FormBuilder.

We need to nestFormBuilders and utilize the array method like this for the skills form control:

this.formBuilder.array([
      this.formBuilder.control('')
    ])

Handle adding new form control by adding a button Add skill which triggers addSkills method defined in the class.

This method in turn should push new form control into existing array of controls.

It needs to know where to look for this array- you may create a skills which will return skills marked as FormArray. Now when we have reference to skills in FormGroup we know where we should push new form control.

  get skills() {
    return this.userInfoGroup.get('skills') as FormArray;
  }

  addSkills() {
    this.skills.push(this.formBuilder.control(''));
  }

Update user information type and view of gathered info so you could see skills you are adding.

If you've finished everything and still you want to do something you may create more forms or start looking at unit tests. Writing tests may actually teach you a lot about what and how you are coding.

In the end your code should look more or less like this on

FormBuilder
getter
StackBlitz