⚠️ This lesson is retired and might contain outdated information.

Create a Simple Angular 2 Component

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 2 years ago

The simplest way to create a new component is again to use the Angular CLI. The Angular CLI can quickly generate components for you to use inside your modules and your templates. This lesson walks through using the Angular CLI to generate an Angular 2 Component then using that Component in your application.

Erkan Buelbuel
Erkan Buelbuel
~ 9 years ago

Why are you writing angular2 twice ? 'angular2/angular2' ?

Ruben
Ruben
~ 8 years ago

It seems like some stuff has changed in the meantime with later releases of Angular 2 and that's probably the reason I'm walking into a problem getting this to work. I'm just seeing a blank screen after loading.

My app component is called angular2-fundamentals-app and looks like this:

import { Component } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'angular2-fundamentals-app',
  template: `
    <div>
        <app-simple-form></app-simple-form>
    </div>
  `,
  styleUrls: ['angular2-fundamentals.component.css']
})
export class Angular2FundamentalsAppComponent {
  title = `Let's get started!`;
}

The simple form component looks like this:

import { Component, OnInit } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'app-simple-form',
  template: `
    <p>
      simple-form Works!
    </p>
  `,
  styles: []
})
export class SimpleFormComponent implements OnInit {

  constructor() {}

  ngOnInit() {
  }

}

I don't see any errors in the console and when I change up the app component to just display some text it works. It just won't render the simple form component.

Any ideas?

~ 8 years ago

I had the same. I fixed by getting the latest Release version of angular-cli.

Check the version of angular-cli you are running by typing

ng --version

if seeing something like v0.0..., you need to reinstall by following instructions in https://github.com/angular/angular-cli#updating-angular-cli.

I didn't bother reinstalling the npm modules in the project, I started from scratch again.

Ruben
Ruben
~ 8 years ago

Thanks mate, that fixed it for me as well. After updating I had to recreate the project, but that did solve the issue I had.

Currently running v1.0.0-beta.20-4 of ng-cli and before it was 0.0.39 I believe.

Danny Goldner
Danny Goldner
~ 8 years ago

Same here. 10x for sharing the solution, but I am trying to install the new version of angular-cli and seems it doesn't update the version I already have 0.1.0 :-/

Update and Solution:

If you have trouble update angular-cli, you probably will need first to update your npm. Follow this:

  1. npm cache clean
  2. npm uninstall -g angular-cli
  3. npm install npm@latest -g
  4. npm install -g angular-cli

Hope it helps!

Cameron
Cameron
~ 8 years ago

Ruben's step was essential, the file configuration was different for me after I updated npm. However, when I set up the framework there were different files than in the video; I also had an app.component.css and an app.component.html file in the app folder. In order to get the special-form.component.ts to function I had to place the app-special-form call in the app.component.html file. Is this correct or is there another way to do this?

Eli
Eli
~ 7 years ago

You refered back to another course/video which was about how to config the CLI config file. Its bot part of this couese. please provide the link to that video

Markdown supported.
Become a member to join the discussionEnroll Today