> npm install -g @angular/cli
> ng help
> ng new todo --directory demo --inline-style --skip-tests --skip-install
> cd demo/
> rm -rf e2e/ karma.conf.js protractor.conf.js README.md
> yarn install
> ng serve
Browse to http://localhost:4200/
> npm install --save todomvc-app-css todomvc-common
"styles": [
"styles.css",
"../node_modules/todomvc-common/base.css",
"../node_modules/todomvc-app-css/index.css"
],
demo-store
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
export class Todo {
id?: string;
title?: string;
completed?: boolean;
constructor(title: string) {
this.completed = false;
this.title = title.trim();
}
}
@Injectable()
export class TodoStore {
todos: Todo[] = [];
constructor() { }
}
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { TodoStore } from './todo.store';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [TodoStore],
bootstrap: [AppComponent]
})
export class AppModule { }
import { Component } from '@angular/core';
import { TodoStore, Todo } from './todo.store';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html'
})
export class AppComponent {
constructor(public store: TodoStore) { }
}
demo-html-skeleton
<section class="todoapp">
<header class="header">
<h1>todos</h1>
</header>
<section class="main">
<ul class="todo-list">
<li *ngFor="let todo of store.todos"
[class.completed]="todo.completed"
[class.editing]="todo.editing">
<div class="view">
<input class="toggle"
type="checkbox" >
<label>{{todo.title}}</label>
</div>
</li>
</ul>
</section>
</section>
> java -jar ../util/swagger-codegen-cli.jar generate -i http://localhost:9000/v2/api-docs -l typescript-angular2 -o src/app/services --model-name-suffix ApiModel
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"swagger": "java -jar ../util/swagger-codegen-cli.jar generate -i http://localhost:9000/v2/api-docs -l typescript-angular2 -o src/app/services --model-name-suffix ApiModel"
},
> npm run swagger
demo-store-import-api
import { TodocontrollerApi, TodoApiModel } from './services';
export class Todo implements TodoApiModel {
demo-store-todos
constructor(public api: TodocontrollerApi) {
this.api.list().subscribe(x => this.set(x));
}
private set(todos: TodoApiModel[]) {
this.todos = todos.map(x => <Todo>x);
}
demo-module-import-api
import { BASE_PATH, TodocontrollerApi } from './services';
demo-module-providers
providers: [
{provide: BASE_PATH, useValue:"."},
TodocontrollerApi,
TodoStore
],
demo-proxy-config
{
"/api/*": {
"target": "http://localhost:9000/",
"secure": false
}
}
> ng serve --proxy-config proxy.config.json
"start": "ng serve --proxy-config proxy.config.json",
demo-html-add
<input class="new-todo"
placeholder="What needs to be done?"
autofocus=""
[(ngModel)]="newTodoText"
(keyup.enter)="addTodo()">
public newTodoText : string;
demo-component-add
addTodo(){
if (this.newTodoText.trim().length) {
this.store.add(this.newTodoText);
this.newTodoText = '';
}
}
demo-store-add
add(title: string) {
let todo = new Todo(title);
this.api.create(todo).subscribe(x => this.set(x));
}
demo-html-complete
<input class="toggle"
type="checkbox"
(click)="toggleCompletion(todo)"
[checked]="todo.completed">
demo-component-complete
toggleCompletion(todo: Todo) {
this.store.complete(todo);
}
demo-store-complete
complete(todo: Todo) {
this.api.complete(todo.id).subscribe(x => this.set(x));
}
demo-html-edit-1
<label (dblclick)="editTodo(todo)">{{todo.title}}</label>
demo-html-edit-2
<input class="edit"
*ngIf="todo.editing"
[value]="todo.title"
#editedtodo
(blur)="completeEditing(todo, editedtodo.value)"
(keyup.enter)="completeEditing(todo, editedtodo.value)"
(keyup.escape)="cancelEditingTodo(todo)">
demo-component-edit1
editTodo(todo: Todo) {
todo.editing = true;
}
demo-component-edit2
cancelEditingTodo(todo: Todo) {
todo.editing = false;
}
demo-component-edit3
completeEditing(todo: Todo, editedTitle: string) {
todo.title = editedTitle;
this.store.update(todo);
}
editing?: boolean;
demo-store-edit
update(todo: Todo){
this.api.update(todo).subscribe(x => this.set(x));
}
demo-html-delete
<button class="destroy" (click)="remove(todo)"></button>
remove(todo: Todo) {
this.store.remove(todo);
}
demo-store-delete
remove(todo: Todo) {
this.api.remove(todo).subscribe(x => this.set(x));
}