Join Justin Schwartzenberger for an in-depth discussion in this video Class constructor injection, part of Angular 2 Essential Training.
- [Instructor] There are two pieces to the puzzle to do constructor injection at a class level for components, directives, and pipes, the import statement and constructor parameters with type annotation. Let's refactor the media item form component class to use the formBuilder class to help in the creation of the FormModel to see constructor injection in action. The formBuilder is a class provided in the Angular platform that has a group method on it for building a form group.
Now we can refactor the ngOnInit method to use the injected formBuilder and call the group method on that, replacing the new form group statement. The formBuilder also has a control method for creating new form control objects, so we can replace the new FormControl calls in here as well using the control method for those. And now we can get rid of the FormGroup and FormControl imports, as we don't need them anymore.
So, not only did we do some constructor injection here, but we also brought in a service instance to help us build the form, allowing us to remove the instantiation calls from within our component. Now this ultimately leads to better decoupled code and is one of the big benefits of dependency injection and inversion of control.
Justin Schwartzenberger steps through the framework one feature at a time, focusing on the component-based architecture of Angular. Learn what Angular is and what it can do, as Justin builds a full-featured web app from start to finish. After mastering the essentials, you can tackle the other project-based courses in our library and create your own Angular app.
- What is Angular?
- Setting up an Angular template
- Creating a component
- Binding events and properties
- Getting data to components
- Using directives and pipes
- Creating Angular forms
- Validating form data
- Understanding dependency injection
- Providing services
- Making HTTP calls
Skill Level Intermediate
Q: This course was updated on 03/01/2018. What changed?
A: The following topics were updated: directives and pipes, forms, dependency injection and services, HTTP, and routing.
1. Architecture Overview
3. Directives and Pipes
5. Dependency Injection and Services
Router links2m 36s
Next steps1m 20s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.