日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Angular的built-in指令

發(fā)布時(shí)間:2023/12/19 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Angular的built-in指令 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Angular built-in指令分為attribute指令和structural指令兩種。

Attribute指令

最常用的attribute指令有NgClass, NgStyle和NgModel三種。

NgClass

動(dòng)態(tài)添加或者刪除html標(biāo)簽的css類(lèi)。

例子:

<!-- toggle the "special" class on/off with a property --> <div [ngClass]="isSpecial ? 'special' : ''">This div is special</div>

isSpecial為true時(shí),div標(biāo)簽打上special的css類(lèi)。

一個(gè)更復(fù)雜一些的例子:

<div [ngClass]="currentClasses">This div is initially saveable, unchanged, and special.</div>

div的class綁定到了Component的property,名為currentClasses.

在Component的實(shí)現(xiàn)代碼里,currentclasses的賦值邏輯:

currentClasses: {}; /* . . . */setCurrentClasses() {// CSS classes: added/removed per current state of component propertiesthis.currentClasses = {saveable: this.canSave,modified: !this.isUnchanged,special: this.isSpecial};}

NgModel

例子:

<label for="example-ngModel">[(ngModel)]:</label> <input [(ngModel)]="currentItem.name" id="example-ngModel">

上面實(shí)際上是一個(gè)雙向綁定的語(yǔ)法糖,等價(jià)于:

<label for="without">without NgModel:</label> <input [value]="currentItem.name" (input)="currentItem.name=$event.target.value" id="without">

Structural指令

ngIf

例子:

<div *ngIf="hero" class="name">{{hero.name}}</div>

實(shí)際是個(gè)語(yǔ)法糖,等價(jià)于:

<ng-template [ngIf]="hero"><div class="name">{{hero.name}}</div> </ng-template>

ngFor

例子:

<app-item-detail *ngFor="let item of items" [item]="item"></app-item-detail>

let item of items的含義:Take each item in the items array, store it in the local item looping variable, and make it available to the templated HTML for each iteration.

ngFor指令有一個(gè)內(nèi)置屬性index:

<div *ngFor="let item of items; let i=index">{{i + 1}} - {{item.name}}</div>

上面的例子,將index屬性賦給template變量i.

總結(jié)

以上是生活随笔為你收集整理的Angular的built-in指令的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。