最近有個小錯誤,因為最近還是在看thingsboard,最近終于看到前端的代碼,突然發現怎么全是ts的文件,仔細一看原來并不是之前認為的AngularJS,而是Angular。。。我tm真的無語了,又要去重新學。。。
Angular的結構比起AngularJS真的復雜很多,以前還可以說是傳統HTML+JS結構的擴展。新的版本真的大變了。
以前的AngularJS只要一個html就是開炫,現在是要一堆文件,就算摸清楚最小系統,也要折騰一番,唉,好吧。。。
1 環境配置
手動配置Angular的環境也是堪稱折磨,尤其是package.json,tsconfig.json。所以一般都用自動配置。
首先是安裝node.js,安裝的原始命令是:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
因為眾所周知的原因,這個命令很大概率要超時,必須換成。
curl -o- https://gitee.com/mirrors/nvm/raw/v0.39.7/install.sh | bash
之后source ~/.bashrc
然后升級nvm install --lts
# 然后全局安裝 Angular CLI
npm i -g @angular/cli
后面用到的ng命令,就是Angular CLI工具。這個工具的幫助如下:?
# 創建項目(這一步 CLI 會自動生成配置和依賴),
ng new hello-angular --minimal --routing=false --style=css
cd hello-angular
ng serve -o ? ? ?# 默認 http://localhost:4200
2 典型的Angular
在上一步生成的代碼基礎上,做了一些修改。如下:?
?main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { App } from './app/app';
import { appConfig } from './app/app.config';bootstrapApplication(App, appConfig).catch((err) => console.error(err));
index.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8" /><title>HelloAngular</title><base href="/" /></head><body><app-root></app-root> <!-- 👈 Angular 根組件掛載點 --></body>
</html>
?app.ts
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common'; // ? 加上這個!
import { TodoService, TodoItem } from './todo.service';@Component({selector: 'app-root',standalone: true,imports: [FormsModule, CommonModule], // ? 把 CommonModule 加入 importstemplateUrl: './app.component.html',styleUrls: ['./app.component.css'],
})
export class App {newTitle = '';constructor(public todo: TodoService) {}add() {if (this.newTitle.trim()) {this.todo.add({ title: this.newTitle.trim(), done: false });this.newTitle = '';}}toggle(item: TodoItem) {this.todo.toggle(item);}remove(item: TodoItem) {this.todo.remove(item);}
}
todo.service.ts
import { Injectable } from '@angular/core';export interface TodoItem {title: string;done: boolean;
}@Injectable({ providedIn: 'root' })
export class TodoService {list: TodoItem[] = [];add(item: TodoItem) { this.list.push(item); }toggle(item: TodoItem) { item.done = !item.done; }remove(item: TodoItem) { this.list = this.list.filter(i => i !== item); }
}
app.config.ts
import { ApplicationConfig, provideBrowserGlobalErrorListeners, provideZoneChangeDetection } from '@angular/core';export const appConfig: ApplicationConfig = {providers: [provideBrowserGlobalErrorListeners(),provideZoneChangeDetection({ eventCoalescing: true }),]
};
app.component.html
<h1>📝 Angular Todo (standalone)</h1><inputplaceholder="輸入待辦事項"[(ngModel)]="newTitle"(keyup.enter)="add()"
/>
<button (click)="add()">添加</button><ul><li *ngFor="let item of todo.list"><input type="checkbox" [checked]="item.done" (change)="toggle(item)" /><span [class.done]="item.done">{{ item.title }}</span><button (click)="remove(item)">🗑</button></li>
</ul>
app.component.css
.done { text-decoration: line-through; color: #888; }
li { margin: 4px 0; }
?
概念 | 代碼位置 | 說明 |
---|---|---|
組件 (Component) | AppComponent | UI 單元 + 邏輯 |
模板 (Template) | app.component.html | HTML + Angular 指令 (*ngFor , [(ngModel)] ) |
服務 (Service) | TodoService | 業務數據與方法,注入到組件 |
注入 (DI) | constructor(public todo: TodoService) | 將服務注入組件 |
雙向綁定 | [(ngModel)]="newTitle" | 表單輸入 ? 組件字段 |
事件綁定 | (click)="add()" | 用戶操作觸發方法 |