ng generate component todos
# or the shorthand version:
ng g c todos
# output
installing component
create src/app/todos/todos.component.css
create src/app/todos/todos.component.html
create src/app/todos/todos.component.spec.ts
create src/app/todos/todos.component.ts
update src/app/app.module.ts
tree -F app/
app/
├── app.component.css
├── app.component.html
├── app.component.ts
├── app.module.ts
└── todos/
├── todos.component.css
├── todos.component.html
├── todos.component.spec.ts
└── todos.component.ts
import { Component } from '@angular/core';
// the Decorator
@Component({
selector: 'app-root',
// the View
template: `
Welcome to {{title}}!!
`,
styles: []
})
export class AppComponent {
// the Controler
title = 'app';
}
// ...
import { TodosComponent } from './todos/todos.component';
@NgModule({
declarations: [
AppComponent,
TodosComponent // declare our Component as member of NgModule
],
@Component({
selector: 'app-todos', // this is our new HTML element
templateUrl: './todos.component.html',
styleUrls: ['./todos.component.css']
})
put an <app-todos></app-todos> element in the parent component (app.component.html in our case)
Welcome to {{title}}!
Let's generate a todos-header component, which should be a child of our todos component
# make sure you are somewhere in you App folder
pwd
# {path}/TODOapp/src/app
ng g component todos/header
# installing component
# create src/app/todos/header/header.component.ts
Make sure you use your new Component
@Component({
selector: 'todos-header',
templateUrl:"header.component.html",
styleUrls: ["header.component.css"]
})
The {{title}} section
You can freely change the pre-generated selector name - just make sure you use the same tag in the parent template
styles
styles
value is an array of strings, each consisting of CSS you want to use with this Component
@Component({
selector: 'app-todos',
templateUrl: "./todos.component.html",
// styleUrls: ["todos.component.css"]
styles: [`
section{
width: 80%;
height: 80vh;
background: #999;
border: 1px solid red;
box-sizing: border-box;
}`]
})
styleUrls
styleUrls
value is an array of URLs to the css files you want to include in this Component
@Component({
selector: 'app-todos',
templateUrl: "./todos.component.html",
styleUrls: ["todos.component.css"]
})
section{
width: 80%;
height: 80vh;
background: #999;
border: 1px solid red;
box-sizing: border-box;
}
export class TodoItemComponent implements OnInit {
itemName: string; // <-- added itemName property
// will be bound to {{itemName}} in template
constructor() {
this.itemName = 'This is my first TODO Item'
}
ngOnInit() {
}
}
will be discussed deeply in next sections
These slides are based on
customised version of
framework