Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Initialize Angular Components with Lifecycle Hooks

    Lukas RuebbelkeLukas Ruebbelke

    Angular exposes key events within a component's lifecycle so that we can apply our own custom logic. In this lesson, we will learn how to leverage component lifecycle hooks to adhere to best practices of keeping our initialization logic out of our constructor.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    In this lesson, we are going to learn about Angular lifecycle hooks and how we can use them in our application. A component has an internal lifecycle that is managed by Angular.

    What I mean by that is Angular is responsible for not only creating the component but rendering it, creating and rendering its children, checking when data bound properties change, and determining what needs to be cleaned up when the component is removed from the DOM.

    Angular offers this component lifecycle hooks that gives us visibility into these key moments so that we can hook into them and perform bits of logic. If we look at the documentation, we can see some of these hooks here -- so ngOnInit(), ngOnDestroy(), and ngOnChanges(). These are the once that we're going to use frequently in Angular 2, but you can see there's a pretty big list here.

    The wonderful thing is that component lifecycle hooks have been ported back to Angular 1.5. We are going to see an example of how this works in an Angular 1 application. In our app.html we're going to create a button. We're going to give it a toggle label. Then from here, on ng-click, we're going to toggle an isVisible property, and then we're going to add an ng-if to our categories component that's going to toggle visibility depending on the value of isVisible.

    Let's hop into the browser and see what this looks like. We'll click toggle; you see it. Click it again, you don't. See it and you do not. This is a perfect candidate to hook lifecycle hooks into our categories component.

    Let's go into our categories controller and we're going to add an OnInit() method and just a simple console log statement, but we'll also add an onDestroy method and we'll do another console log so that this gets called when the element is removed from the DOM.

    Now that we've defined our lifecycle event hooks, let's put up the developer console. You can see as we toggle this, we are getting our console logs shown to us. This is an adorable example. But what is a practical use for lifecycle hooks? One of the main reasons why we use this is because its best practice to remove any initialization logic out of the constructor.

    We're going to move this categories model, getCategories call into the OnInit() event hook, and then we're going to reference this.categories model = categories model. Then also we need to reference this.categories model within our OnInit() lifecycle hook. We'll clean up the DOM here.

    Now that this is cleaned up, let's hop into the browser and you can see now that we were able to get the categories but we did it from the OnInit() lifecycle hook.

    This is especially important when you have a component, a sub-component that has a dependency on a parent component that has properties that may not exist because they're part of an asynchronous operation. This is why you want to move these two in OnInit() lifecycle hook. This is how you leverage event lifecycle hooks within an Angular 1 application.