ng new task-manager
cd task-manager
ng serve
Visit http://localhost:4200 — you should see "Welcome to task-manager!".
ng generate component task-list
# or shortcut
ng g c task-list
This creates:
import { Component } from '@angular/core';
@Component({
selector: 'app-task-list',
templateUrl: './task-list.component.html',
styleUrls: ['./task-list.component.css']
})
export class TaskListComponent {
tasks: string[] = ['Learn Angular', 'Build a project', 'Deploy app'];
addTask(newTask: string) {
if (newTask) {
this.tasks.push(newTask);
}
}
}
<h2>Task Manager</h2>
<input #taskInput placeholder="Add a new task">
<button (click)="addTask(taskInput.value); taskInput.value=''">Add Task</button>
<ul>
<li *ngFor="let task of tasks">
{{ task }}
</li>
</ul>
<app-task-list></app-task-list>
ng generate service task
# or shortcut
ng g s task
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class TaskService {
private tasks: string[] = [];
getTasks() {
return this.tasks;
}
addTask(task: string) {
if (task) {
this.tasks.push(task);
}
}
}
TaskManagerApp
├── AppComponent
│ └── TaskListComponent
│ └── Uses TaskService for data