Become a member
to unlock all features

Level Up!

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


    Create an Angular 2 Hello World Component

    2 - 6

    A Hello World application in Angular 2 requires you to understand concepts around bootstrapping, modules, and components. The bootstrapping system allows you to decide which module and platform you’ll use when your app starts up as well as which components should be shown first. The Angular 2 modules define which features you’ll want to include so you can have fine control over your final project size. The Angular 2 Components then become a simple definition of a template with variables.



    Become a Member to view code

    You must be a Pro 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
    orLog In




    Our systems JS configuration says we need an App folder with a main file inside of it, which it will load and compile as the default file, so let's go ahead and create that.

    App/main.ts. From here we want to create our component, so let's say App component, and each class that's a component will need the component decorator.

    The component decorator starts with a selector. We'll call this App. What this means is that this App needs to match in our index html, a tag on this page.

    We'll say App, and that matches with this selector of App. So inside of here, this content will be replaced. We'll just say loading, and then once this is loaded, then that loading message will go away.

    Back in our main, let's make a template of Div saying I'm the App component. We're not doing anything with this component yet.

    We actually need to bootstrap it inside of a module, so go ahead and export a class called App module, and decorate that with ng module.

    Inside of my ng module, I need to say what I'm going to declare. The components I'm going to use. These are called declarations, and I want to use my App component, and I also want to treat that as the bootstrap component.

    So we'll bootstrap App component. Then there's also a module we'll want to use because we're working inside of the browser, and that's declared inside the imports, and that's the browser module.

    Because you can target multiple platforms other than the browser, you need to explicitly state that you want to target the browser. Then finally to bootstrap, we'll use the platform browser dynamic method.

    We'll invoke it and say .bootstrapmodule, and we'll bootstrap the App module. When I hit save here, you'll see loading, and then I'm the App component, because our platform is bootstrapping this module. This module is configured to use the browser.

    It's declaring the App component, so it'll look up that selector and use it, and because this is the application module, we're saying to bootstrap this right away.

    Then, inside of this component, we're saying find the selector called App, and drop in this Div inside of there to match the index html loading.