結(jié)構(gòu)指令通過(guò)添加,替換和刪除其元素來(lái)更改DOM的布局。 結(jié)構(gòu)指令的兩個(gè)常見(jiàn)示例如下所示:
NgFor:這是一個(gè)自定義數(shù)據(jù)顯示的repeater指令。 它可用于顯示項(xiàng)目列表。
NgIf:它根據(jù)表達(dá)式求值刪除或重新創(chuàng)建DOM樹(shù)的一部分。
下面的例子描述了使用Angular 2中的結(jié)構(gòu)指令:
<html> <head> <title>Contact Form</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="/attachments/w3c/es6-shim.min.js"></script> <script src="/attachments/w3c/system-polyfills.js"></script> <script src="/attachments/w3c/angular2-polyfills.js"></script> <script src="/attachments/w3c/system.js"></script> <script src="/attachments/w3c/typescript.js"></script> <script src="/attachments/w3c/Rx.js"></script> <script src="/attachments/w3c/angular2.dev.js"></script> <script> System.config({ transpiler: 'typescript', typescriptOptions: { emitDecoratorMetadata: true }, packages: {'app': {defaultExtension: 'ts'}} }); System.import('/angular2/src/app/structural_main') .then(null, console.error.bind(console)); </script> </head> <body> <my-app>Loading...</my-app> </body> </html>
上述代碼包括以下配置選項(xiàng):
您可以使用typescript版本配置index.html文件。在使用transpiler選項(xiàng)運(yùn)行應(yīng)用程序之前,SystemJS將TypeScript轉(zhuǎn)換為JavaScript。
如果在運(yùn)行應(yīng)用程序之前沒(méi)有翻譯到JavaScript,您可能會(huì)看到瀏覽器中隱藏的編譯器警告和錯(cuò)誤。
當(dāng)設(shè)置了 emitDecoratorMetadata 選項(xiàng)時(shí),TypeScript會(huì)為代碼的每個(gè)類生成元數(shù)據(jù)。 如果不指定此選項(xiàng),將生成大量未使用的元數(shù)據(jù),這會(huì)影響文件大小和對(duì)應(yīng)用程序運(yùn)行時(shí)的影響。
Angular 2包括來(lái)自app文件夾的包,其中文件將具有.ts擴(kuò)展名。
接下來(lái)它將從應(yīng)用程序文件夾加載主組件文件。如果沒(méi)有找到主要組件文件,那么它將在控制臺(tái)中顯示錯(cuò)誤。
當(dāng)Angular調(diào)用main.ts中的引導(dǎo)函數(shù)時(shí),它讀取Component元數(shù)據(jù),找到“app”選擇器,找到一個(gè)名為app的元素標(biāo)簽,并在這些標(biāo)簽之間加載應(yīng)用程序。
要運(yùn)行代碼,您需要以下TypeScript(.ts)文件,您需要保存在應(yīng)用程序文件夾下。
structural_main.tsimport {bootstrap} from 'angular2/platform/browser'; //importing bootstrap function import {AppComponent} from './structural_app.component'; //importing component function bootstrap(AppComponent);
現(xiàn)在我們將在TypeScript(.ts)文件中創(chuàng)建一個(gè)組件,我們將為該組件創(chuàng)建一個(gè)視圖。
structural_app.component.tsimport {Component} from 'angular2/core'; @Component({ selector: 'my-app', template: ` <h2>{{title}}</h2> <p class="alert alert-success" *ngIf="names.length > 2">Currently there are more than 2 names!</p> <p class="alert alert-danger" *ngIf="names.length <= 2">Currently there are less than 2 names left!</p> <ul> <li *ngFor="#nam of names" (click)="onNameClicked(nam)" >{{ nam.name }}</li> </ul> <input type="text" [(ngModel)]="selectedName.name"> <button (click)="onDeleteName()">Delete Name</button><br><br> <input type="text" #nam> <button (click)="onAddName(nam)">Add Name</button> ` }) export class AppComponent { title = 'Structural Directives'; public names = [ { name: "Kamal"}, { name: "Mitchel"}, { name: "Yoon"}, { name: "Johnson"}, { name: "Jet Li"} ]; public selectedName = {name : ""}; onNameClicked(nam) { this.selectedName = nam; } onAddName(nam) { this.names.push({name: nam.value}); } onDeleteName() { this.names.splice(this.names.indexOf(this.selectedName), 1); this.selectedName.name = ""; } }
@Component是一個(gè)裝飾器,它使用配置對(duì)象來(lái)創(chuàng)建組件及其視圖。
選擇器創(chuàng)建組件的實(shí)例,在父HTML中找到<my-app>標(biāo)記。
Next是* ngFor指令創(chuàng)建我們?cè)谀0逯薪壎ǖ囊晥D導(dǎo)出。 *是使用Angular 2模板語(yǔ)法與模板標(biāo)記的縮寫。
局部變量nam可以在模板中引用并獲取數(shù)組的索引。 當(dāng)你點(diǎn)擊項(xiàng)目值,onNameClicked()事件將獲得激活,Angular 2將綁定模型名稱從數(shù)組與模板的局部變量。
方法onAddName()和onDeleteName()用于從列表中添加和刪除項(xiàng)目。onNameClicked()方法使用局部變量'nam'作為參數(shù),并使用selectedName對(duì)象顯示所選項(xiàng)目。
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將上面的HTML代碼保存為index.html文件,如同我們?cè)?a href="http://m.hgci.cn/angular2/angular2_environment.html" target="_blank">環(huán)境章節(jié)中創(chuàng)建的,并使用上面的包含.ts文件的應(yīng)用程序文件夾。
打開(kāi)終端窗口并輸入以下命令:
npm start
稍后,瀏覽器選項(xiàng)卡應(yīng)打開(kāi)并顯示輸出,如下所示。
或者你可以用另一種方式運(yùn)行這個(gè)文件:
將上面的HTML代碼另存為服務(wù)器根文件夾中的 structural_directives.html 文件。
將此HTML文件打開(kāi)為http://localhost/structural_directives.html,并顯示如下所示的輸出。
更多建議: