响应式
声明状态
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-name",
template: `<h1>你好 {{ name }}</h1>`,
})
export class NameComponent {
name = "张三";
}
@NgModule({
declarations: [NameComponent],
exports: [NameComponent],
})
export class NameModule {}
Aurelia 1
<template>
<h1>你好 ${name}</h1>
</template>
更新状态
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-name",
template: `<h1>你好 {{ name }}</h1>`,
})
export class NameComponent {
name = "张三";
constructor() {
this.name = "李四";
}
}
@NgModule({
declarations: [NameComponent],
exports: [NameComponent],
})
export class NameModule {}
Aurelia 1
<template>
<h1>你好 ${name}</h1>
</template>
计算属性
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-doublecount",
template: `<div>{{ doubleCount }}</div>`,
})
export class DoublecountComponent {
count = 10;
get doubleCount() {
return this.count * 2;
}
}
@NgModule({
declarations: [DoublecountComponent],
exports: [DoublecountComponent],
})
export class DoublecountModule {}
Aurelia 1
<template>
<div>${doubleCount}</div>
</template>
模板语法
最简模板
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-helloworld",
template: `<h1>你好,世界</h1>`,
})
export class HelloworldComponent {}
@NgModule({
declarations: [HelloworldComponent],
exports: [HelloworldComponent],
})
export class HelloworldModule {}
Aurelia 1
<template>
<h1>你好,世界</h1>
</template>
样式
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-cssstyle",
template: `
<h1 class="title">我是红色的</h1>
<button style="font-size: 10rem">我是一个按钮</button>
`,
styles: [
`
.title {
color: red;
}
`,
],
})
export class CssStyleComponent {}
@NgModule({
declarations: [CssStyleComponent],
exports: [CssStyleComponent],
})
export class CssStyleModule {}
Aurelia 1
.title {
color: red;
}
DOM循环
Angular
import { Component, NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
@Component({
selector: "app-colors",
template: `
<ul>
<li *ngFor="let color of colors">{{ color }}</li>
</ul>
`,
})
export class ColorsComponent {
colors = ["红", "绿", "蓝"];
}
@NgModule({
declarations: [ColorsComponent],
imports: [CommonModule],
exports: [ColorsComponent],
})
export class ColorsModule {}
Aurelia 1
<template>
<ul>
<li repeat.for="color of colors">${color}</li>
</ul>
</template>
点击事件
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-counter",
template: `
<p>计数器: {{ count }}</p>
<button (click)="incrementCount()">+1</button>
`,
})
export class CounterComponent {
count = 0;
incrementCount() {
this.count++;
}
}
@NgModule({
declarations: [CounterComponent],
exports: [CounterComponent],
})
export class CounterModule {}
Aurelia 1
<template>
<p>计数器: ${count}</p>
<button click.trigger="incrementCount()">+1</button>
</template>
DOM引用
Angular
import {
Component,
ViewChild,
ElementRef,
OnInit,
NgModule,
} from "@angular/core";
@Component({
selector: "app-inputfocused",
template: `<input type="text" #inputRef />`,
})
export class InputfocusedComponent implements OnInit {
@ViewChild("inputRef", { static: true })
inputRef!: ElementRef<HTMLInputElement>;
ngOnInit() {
this.inputRef.nativeElement.focus();
}
}
@NgModule({
declarations: [InputfocusedComponent],
exports: [InputfocusedComponent],
})
export class InputfocusedModule {}
Aurelia 1
<template>
<input ref="inputElement" />
</template>
条件渲染
Angular
import { Component, NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
const TRAFFIC_LIGHTS = ["红灯", "黄灯", "绿灯"];
@Component({
selector: "app-trafficlight",
template: `
<button (click)="nextLight()">下一个灯</button>
<p>现在亮着的是: {{ light }}</p>
<p>
你应该
<ng-container [ngSwitch]="light">
<span *ngSwitchCase="'红灯'">停下</span>
<span *ngSwitchCase="'黄灯'">慢行</span>
<span *ngSwitchCase="'绿灯'">赶紧走</span>
</ng-container>
</p>
`,
})
export class TrafficlightComponent {
lightIndex = 0;
get light() {
return TRAFFIC_LIGHTS[this.lightIndex];
}
nextLight() {
this.lightIndex = (this.lightIndex + 1) % TRAFFIC_LIGHTS.length;
}
}
@NgModule({
declarations: [TrafficlightComponent],
imports: [CommonModule],
exports: [TrafficlightComponent],
})
export class TrafficlightModule {}
Aurelia 1
<template>
<button click.trigger="nextLight()">下一个灯</button>
<p>现在亮着的是: ${light}</p>
<p>
你应该
<span if.bind="light === '红灯'">停下</span>
<span if.bind="light === '黄灯'">慢行</span>
<span if.bind="light === '绿灯'">赶紧走</span>
</p>
</template>
生命周期
组件加载时
Angular
import { Component, NgModule, OnInit } from "@angular/core";
@Component({
selector: "app-pagetitle",
template: `<p>页面标题: {{ pageTitle }}</p>`,
})
export class PagetitleComponent implements OnInit {
pageTitle = "";
ngOnInit() {
this.pageTitle = document.title;
}
}
@NgModule({
declarations: [PagetitleComponent],
exports: [PagetitleComponent],
})
export class PagetitleModule {}
Aurelia 1
<template>
<p>页面标题: ${pageTitle}</p>
</template>
组件卸载时
Angular
import { Component, NgModule, OnDestroy } from "@angular/core";
@Component({
selector: "app-time",
template: `<p>当前时间: {{ time }}</p>`,
})
export class TimeComponent implements OnDestroy {
time = new Date().toLocaleTimeString();
timer = setInterval(() => {
this.time = new Date().toLocaleTimeString();
}, 1000);
ngOnDestroy() {
clearInterval(this.timer);
}
}
@NgModule({
declarations: [TimeComponent],
exports: [TimeComponent],
})
export class TimeModule {}
Aurelia 1
<template>
<p>当前时间: ${time}</p>
</template>
组件组合
组件Props传参
Angular
import { Component, NgModule } from "@angular/core";
import { UserprofileModule } from "./userprofile.component";
@Component({
selector: "app-root",
template: `
<app-userprofile
name="张三"
[age]="20"
[favouriteColors]="['green', 'blue', 'red']"
[isAvailable]="true"
>
</app-userprofile>
`,
})
export class AppComponent {}
@NgModule({
declarations: [AppComponent],
imports: [UserprofileModule],
bootstrap: [AppComponent],
})
export class AppModule {}
Aurelia 1
<template>
<require from="./user-profile"></require>
<user-profile
name.bind="name"
age.bind="age"
favourite-colors.bind="colors"
is-available.bind="available"
></user-profile>
</template>
向父组件emit事件
Angular
import { Component, NgModule } from "@angular/core";
import { AnswerButtonModule } from "./answer-button.component";
@Component({
selector: "app-root",
template: `
<p>你快乐吗?</p>
<app-answer-button (yes)="onAnswerYes()" (no)="onAnswerNo()">
</app-answer-button>
<p style="font-size: 50px">{{ isHappy ? "😀" : "😥" }}</p>
`,
})
export class AppComponent {
isHappy = true;
onAnswerYes() {
this.isHappy = true;
}
onAnswerNo() {
this.isHappy = false;
}
}
@NgModule({
declarations: [AppComponent],
imports: [AnswerButtonModule],
bootstrap: [AppComponent],
})
export class AppModule {}
Aurelia 1
<template>
<require from="./answer-button"></require>
<p>你快乐吗?</p>
<answer-button action-handler.call="handleAnswer(reply)"></answer-button>
<p style="font-size: 50px">${isHappy ? "😀" : "😥"}</p>
</template>
slot插槽
Angular
import { Component, NgModule } from "@angular/core";
import { FunnyButtonModule } from "./funny-button.component";
@Component({
selector: "app-root",
template: `<app-funny-button>点我!</app-funny-button>`,
})
export class AppComponent {}
@NgModule({
declarations: [AppComponent],
imports: [FunnyButtonModule],
bootstrap: [AppComponent],
})
export class AppModule {}
Aurelia 1
<template>
<require from="./funny-button"></require>
<funny-button>点我!</funny-button>
</template>
slot插槽内容
Angular
import { Component, NgModule } from "@angular/core";
@Component({
selector: "app-root",
template: `
<app-funny-button></app-funny-button>
<app-funny-button>
<ng-template #content>这是传到slot的内容!</ng-template>
</app-funny-button>
`,
})
export class AppComponent {}
@NgModule({
declarations: [AppComponent],
imports: [FunnyButtonModule],
bootstrap: [AppComponent],
})
export class AppModule {}
Aurelia 1
<template>
<require from="./funny-button"></require>
<funny-button></funny-button>
<funny-button>这是传到slot的内容!</funny-button>
</template>
上下文
Angular
import { Component, NgModule } from "@angular/core";
import { UserService } from "./user.service";
import { UserProfileModule } from "./user-profile.component";
@Component({
providers: [UserService],
selector: "app-root",
template: `
<h1>欢迎回来, {{ userService.user.username }}</h1>
<app-user-profile />
`,
})
export class AppComponent {
constructor(public userService: UserService) {}
}
@NgModule({
declarations: [AppComponent],
imports: [UserProfileModule],
bootstrap: [AppComponent],
})
export class AppModule {}
Aurelia 1
缺少代码片段 帮助我们改进 Component Party 
表单输入
输入框
Angular
import { Component, NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
@Component({
selector: "app-input-hello",
template: `
<p>{{ text }}</p>
<input [(ngModel)]="text" />
`,
})
export class InputHelloComponent {
text = "";
}
@NgModule({
declarations: [InputHelloComponent],
imports: [FormsModule],
exports: [InputHelloComponent],
})
export class InputHelloModule {}
Aurelia 1
<template>
<p>${text}</p>
<input value.bind="text" />
</template>
复选框
Angular
import { Component, NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
@Component({
selector: "app-is-available",
template: `
<input id="is-available" type="checkbox" [(ngModel)]="isAvailable" />
<label for="is-available">这是一个checkbox</label>
`,
})
export class IsAvailableComponent {
isAvailable = false;
}
@NgModule({
declarations: [IsAvailableComponent],
imports: [FormsModule],
exports: [IsAvailableComponent],
})
export class IsAvailableModule {}
Aurelia 1
<template>
<input id="is-available" type="checkbox" checked.bind="isAvailable" />
<label for="is-available">这是一个checkbox</label>: ${isAvailable}
</template>
单选框
Angular
import { Component, NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
@Component({
selector: "app-pick-pill",
template: `
<div>你选择了: {{ picked }}</div>
<input id="blue-pill" type="radio" value="blue" [(ngModel)]="picked" />
<label for="blue-pill">蓝色</label>
<input id="red-pill" type="radio" value="red" [(ngModel)]="picked" />
<label for="red-pill">红色</label>
`,
})
export class PickPillComponent {
picked = "red";
}
@NgModule({
declarations: [PickPillComponent],
imports: [FormsModule],
exports: [PickPillComponent],
})
export class PickPillModule {}
Aurelia 1
<template>
<div>你选择了: ${picked}</div>
<input id="blue-pill" checked.bind="picked" type="radio" value="blue" />
<label for="blue-pill">蓝色</label>
<input id="red-pill" checked.bind="picked" type="radio" value="red" />
<label for="red-pill">红色</label>
</template>
选择器
Angular
import { Component, NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
@Component({
selector: "app-color-select",
template: `
<select [(ngModel)]="selectedColorId">
<option
*ngFor="let color of colors"
[value]="color.id"
[disabled]="color.isDisabled"
>
{{ color.text }}
</option>
</select>
`,
})
export class ColorSelectComponent {
selectedColorId = 2;
colors = [
{ id: 1, text: "红" },
{ id: 2, text: "蓝" },
{ id: 3, text: "绿" },
{ id: 4, text: "灰", isDisabled: true },
];
}
@NgModule({
declarations: [ColorSelectComponent],
imports: [FormsModule],
exports: [ColorSelectComponent],
})
export class ColorSelectModule {}
Aurelia 1
<template>
<select value.bind="selectedColorId">
<option value="">选择一个颜色</option>
<option
repeat.for="color of colors"
value.bind="color.id"
disabled.bind="color.isDisabled"
>
${color.text}
</option>
</select>
</template>
Web应用特性
渲染应用
Angular
<!doctype html>
<html>
<body>
<app-root></app-root>
</body>
</html>
Aurelia 1
缺少代码片段 帮助我们改进 Component Party 
fetch获取数据
Angular
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { BehaviorSubject } from "rxjs";
export interface UsersState {
users: User[];
error: string | null;
loading: boolean;
}
export const initialState: UsersState = {
users: [],
error: null,
loading: false,
};
@Injectable({ providedIn: "root" })
export class UserService {
private state = new BehaviorSubject<UsersState>(initialState);
state$ = this.state.asObservable();
constructor(private http: HttpClient) {}
loadUsers() {
this.state.next({ ...initialState, loading: true });
this.http
.get<UserResponse>("https://randomuser.me/api/?results=3")
.subscribe({
next: ({ results }) =>
this.state.next({ ...initialState, users: results }),
error: (error) => this.state.next({ ...initialState, error }),
});
}
}
export interface UserResponse {
results: User[];
info: any;
}
export interface User {
name: {
title: string;
first: string;
last: string;
};
picture: {
large: string;
medium: string;
thumbnail: string;
};
}
Aurelia 1
<template>
<p if.bind="isLoading">正在获取用户信息……</p>
<p if.bind="error">获取用户信息出错</p>
<ul if.bind="users">
<li repeat.for="user of users">
<img src.bind="user.picture.thumbnail" alt="user" />
<p>${ user.name.first } ${ user.name.last }</p>
</li>
</ul>
</template>