一文淺析angular中的組件模板
本篇文章帶大家了解一下angular中的元件模板,簡單介紹一下相關知識點:資料綁定、屬性綁定、事件綁定、雙向資料綁定、內容投影等等,希望對大家有幫助!
Angular 是一個使用HTML
、CSS
、TypeScript
建置客戶端
應用的框架,用來建立單頁
應用程式。 【相關教學推薦:《angular教學》】
Angular 是一個重量級
的框架,內部整合了大量開箱即用
的功能模組。
Angular 為大型應用開發而設計,提供了乾淨且鬆散耦合的程式碼組織方式,使應用程式整潔更易於維護。
angualr文檔:
Angular:https://angular.io/
Angular 中文:https:// angular.cn/
Angular CLI:https://cli.angular.io/
- Angular CLI 中文:https://angular.cn/ cli
元件模板
#1、資料綁定
資料綁定就是將元件類別中的資料顯示在元件範本中,當元件類別中的資料改變時會自動被同步到元件範本中(資料驅動DOM )。
在 Angular 中使用插值表達式
進行資料綁定,即 {<!-- -->{ }}
。
<h2>{{message}}</h2> <h2>{{getInfo()}}</h2> <h2>{{a == b ? '相等': '不等'}}</h2> <h2>{{'Hello Angular'}}</h2> <p [innerHTML]="htmlSnippet"></p> <!-- 对数据中的代码进行转义 -->
2、屬性綁定
#2.1 一般屬性
屬性綁定分為兩種情況,綁定DOM 物件屬性
和綁定HTML標記屬性
。
使用
[屬性名稱]
為元素綁定 DOM 物件屬性。<img [src]="imgUrl"/>
登入後複製使用
[attr.屬性名稱]
為元素綁定HTML 標記屬性<td [attr.colspan]="colSpan"></td>
登入後複製
在大多數情況下,DOM 物件屬性和HTML 標記屬性是對應的關係,所以使用第一種情況。
但是某些屬性只有HTML 標記
存在,DOM 物件中不存在,此時需要使用第二種情況,例如colspan
屬性,在DOM 物件中就沒有。
或自訂 HTML 屬性也需要使用第二種情況。
2.2 class 屬性
<button class="btn btn-primary" [class.active]="isActive">按钮</button> <div [ngClass]="{'active': true, 'error': true}"></div>
2.3 style 屬性
<button [style.backgroundColor]="isActive ? 'blue': 'red'">按钮</button> <button [ngStyle]="{'backgroundColor': 'red'}">按钮</button>
3、事件綁定
<button (click)="onSave($event)">按钮</button> <!-- 当按下回车键抬起的时候执行函数 --> <input type="text" (keyup.enter)="onKeyUp()"/>
export class AppComponent { title = "test" onSave(event: Event) { // this 指向组件类的实例对象 this.title // "test" } }
4、取得原生DOM 物件
##4.1 在元件範本中取得 #
<input type="text" (keyup.enter)="onKeyUp(username.value)" #username/>
4.2 在元件類別中取得
使用ViewChild 裝飾器取得一個元素
<p #paragraph>home works!</p>
import { AfterViewInit, ElementRef, ViewChild } from "@angular/core" export class HomeComponent implements AfterViewInit { @ViewChild("paragraph") paragraph: ElementRef<HTMLParagraphElement> | undefined ngAfterViewInit() { console.log(this.paragraph?.nativeElement) } }
ViewChildren 取得一組元素
<ul> <li #items>a</li> <li #items>b</li> <li #items>c</li> </ul>
import { AfterViewInit, QueryList, ViewChildren } from "@angular/core" @Component({ selector: "app-home", templateUrl: "./home.component.html", styles: [] }) export class HomeComponent implements AfterViewInit { @ViewChildren("items") items: QueryList<HTMLLIElement> | undefined ngAfterViewInit() { console.log(this.items?.toArray()) } }
5、雙向資料綁定
資料在元件類別和元件模板中雙向同步。 Angular 將雙向資料綁定功能放在了@angular/forms 模組中,所以要實現雙向資料綁定需要依賴該模組。
import { FormsModule } from "@angular/forms" @NgModule({ imports: [FormsModule], }) export class AppModule {}
<input type="text" [(ngModel)]="username" /> <button (click)="change()">在组件类中更改 username</button> <div>username: {{ username }}</div>
export class AppComponent { username: string = "" change() { this.username = "hello Angular" } }
6、內容投影#
<!-- app.component.html --> <bootstrap-panel> <div class="heading test"> Heading </div> <div class="body"> Body </div> </bootstrap-panel>
<!-- panel.component.html --> <div class="panel panel-default"> <div class="panel-heading"> <ng-content select=".heading"></ng-content> </div> <div class="panel-body"> <ng-content select=".body"></ng-content> </div> </div>
取代,如果不想要這個額外的div,可以使用ng -container替代這個div。
- ng-content 通常在投影中使用:當父元件需要向子元件投影資料時必須指定將資料投影到子元件的哪個位置,這時候就可以利用ng-content標籤來做一個佔位符,不會產生真實的dom元素,只會把投影的內容copy過來。 ng-container是一個特殊的容器標籤,不會產生真實的dom元素,所以在ng-container標籤中添加屬性是無效的。
<!-- app.component.html --> <bootstrap-panel> <ng-container class="heading"> Heading </ng-container> <ng-container class="body"> Body </ng-container> </bootstrap-panel>
7、資料綁定容錯處理#
// app.component.ts export class AppComponent { task = { person: { name: '张三' } } }
<!-- 方式一 --> <span *ngIf="task.person">{{ task.person.name }}</span> <!-- 方式二 --> <span>{{ task.person?.name }}</span>
8、全域樣式#
/* 第一种方式 在 styles.css 文件中 */ @import "~bootstrap/dist/css/bootstrap.css"; /* ~ 相对node_modules文件夹 */
<!-- 第二种方式 在 index.html 文件中 --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet" />
// 第三种方式 在 angular.json 文件中 "styles": [ "./node_modules/bootstrap/dist/css/bootstrap.min.css", "src/styles.css" ]
程式設計影片! !
以上是一文淺析angular中的組件模板的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

這篇文章帶大家繼續angular的學習,簡單了解一下Angular中的獨立組件(Standalone Component),希望對大家有幫助!

Angular.js是一種可自由存取的JavaScript平台,用於建立動態應用程式。它允許您透過擴展HTML的語法作為模板語言,以快速、清晰地表示應用程式的各個方面。 Angular.js提供了一系列工具,可協助您編寫、更新和測試程式碼。此外,它還提供了許多功能,如路由和表單管理。本指南將討論在Ubuntu24上安裝Angular的方法。首先,您需要安裝Node.js。 Node.js是一個基於ChromeV8引擎的JavaScript運行環境,可讓您在伺服器端執行JavaScript程式碼。要在Ub

這篇文章跟大家分享一個Angular實戰,了解一下angualr 結合 ng-zorro 如何快速開發一個後台系統,希望對大家有幫助!

angular中怎麼使用monaco-editor?以下這篇文章記錄下最近的一次業務中用到的 monaco-editor 在 angular 中的使用,希望對大家有幫助!

隨著網路的快速發展,前端開發技術也不斷改進與迭代。 PHP和Angular是兩種廣泛應用於前端開發的技術。 PHP是一種伺服器端腳本語言,可以處理表單、產生動態頁面和管理存取權限等任務。而Angular是一種JavaScript的框架,可以用來開發單一頁面應用程式和建構元件化的網頁應用程式。本篇文章將介紹如何使用PHP和Angular進行前端開發,以及如何將它們

這篇文章帶大家了解Angular中的獨立元件,看看怎麼在Angular中建立一個獨立元件,怎麼在獨立元件中導入已有的模組,希望對大家有幫助!
