搜尋
首頁web前端js教程詳解Angular中的模板文法

詳解Angular中的模板文法

Apr 23, 2021 am 10:37 AM
angular

本篇文章給大家詳細介紹一下Angular中的範本語法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有幫助。

詳解Angular中的模板文法

相關教學推薦:《angular教學

##插值表達式

    test-interpolation.component.ts
  • @Component({
      selector: 'app-test-interpolation',
      templateUrl: './test-interpolation.component.html',
      styleUrls: ['./test-interpolation.component.css']
    })
    export class TestInterpolationComponent implements OnInit {
    
      title = '插值表达式';
    
      constructor() { }
    
      ngOnInit() {
      }
    
      getValue(): string {
        return '值';
      }
    }
    test-interpolation.component.html
  • <div class="panel panel-primary">
      <div class="panel-heading">基插值语法</div>
      <div class="panel-body">
        <h3>
          欢迎来到 {{title}}!
        </h3>
        <h3 id="nbsp-nbsp-nbsp-nbsp">2+2 = {{2 + 2}}</h3>
        <h3 id="调用方法-getValue">调用方法{{getValue()}}</h3>
      </div>
    </div>

模板變數

    test-template-variables.component.ts
  • @Component({
      selector: &#39;app-test-template-variables&#39;,
      templateUrl: &#39;./test-template-variables.component.html&#39;,
      styleUrls: [&#39;./test-template-variables.component.css&#39;]
    })
    export class TestTempRefVarComponent implements OnInit {
    
      constructor() { }
    
      ngOnInit() {
      }
    
      public saveValue(value: string): void {
        console.log(value);
      }
    }
    test-template-variables.component.html
  • #
    <div class="panel panel-primary">
      <div class="panel-heading">模板变量</div>
      <div class="panel-body">
        <input #templateInput>
        <p>{{templateInput.value}}</p>
        <button class="btn btn-success" (click)="saveValue(templateInput.value)">局部变量</button>
      </div>
    </div>

#值綁定、事件綁定、雙向綁定

值綁定:[]

    #test-value-bind.component.ts
  • # #
    @Component({
      selector: &#39;app-test-value-bind&#39;,
      templateUrl: &#39;./test-value-bind.component.html&#39;,
      styleUrls: [&#39;./test-value-bind.component.css&#39;]
    })
    export class TestValueBindComponent implements OnInit {
    
      public imgSrc = &#39;./assets/imgs/1.jpg&#39;;
    
      constructor() { }
    
      ngOnInit() {
      }
    }
test-value-bind.component.html
  • <div class="panel panel-primary">
      <div class="panel-heading">单向值绑定</div>
      <div class="panel-body">
        <img  [src]="imgSrc" / alt="詳解Angular中的模板文法" >
      </div>
    </div>
事件綁定:()

##test- event-bind-component.ts

    @Component({
      selector: &#39;app-test-event-binding&#39;,
      templateUrl: &#39;./test-event-binding.component.html&#39;,
      styleUrls: [&#39;./test-event-binding.component.css&#39;]
    })
    export class TestEventBindingComponent implements OnInit {
    
      constructor() { }
    
      ngOnInit() {
      }
    
      public btnClick(event: any): void {
        console.log(event + &#39;测试事件绑定!&#39;);
      }
    }
  • test-event-bind.component.html
    <div>
        <div>事件绑定</div>
        <div>
            <button>点击按钮</button>
        </div>
    </div>
  • 雙向綁定:[()]

    test-twoway-binding.component.ts

      #
      @Component({
        selector: &#39;app-test-twoway-binding&#39;,
        templateUrl: &#39;./test-twoway-binding.component.html&#39;,
        styleUrls: [&#39;./test-twoway-binding.component.css&#39;]
      })
      export class TestTwowayBindingComponent implements OnInit {
      
        public fontSizePx = 14;
      
        constructor() { }
      
        ngOnInit() {
        }
      
      }
    • test-twoway-binding.component.html
      #
      <div class="panel panel-primary">
        <div class="panel-heading">双向绑定</div>
        <div class="panel-body">
          <app-font-resizer [(size)]="fontSizePx"></app-font-resizer>
          <div [style.font-size.px]="fontSizePx">Resizable Text</div>
        </div>
      </div>
    • font-resizer.component.ts
      @Component({
        selector: &#39;app-font-resizer&#39;,
        templateUrl: &#39;./font-resizer.component.html&#39;,
        styleUrls: [&#39;./font-resizer.component.css&#39;]
      })
      export class FontResizerComponent implements OnInit {
      
        @Input()
        size: number | string;
      
        @Output()
        sizeChange = new EventEmitter<number>();
      
        constructor() { }
      
        ngOnInit() {
        }
      
        decrement(): void {
          this.resize(-1);
        }
      
        increment(): void {
          this.resize(+1);
        }
      
        resize(delta: number) {
          this.size = Math.min(40, Math.max(8, +this.size + delta));
          this.sizeChange.emit(this.size);
        }
      }
    • font-resizer.component.html
      <div style="border: 2px solid #333">
        <p>这是子组件</p>
        <button (click)="decrement()" title="smaller">-</button>
        <button (click)="increment()" title="bigger">+</button>
        <label [style.font-size.px]="size">FontSize: {{size}}px</label>
      </div>
    • 內建結構型指令

    *ngIf

    test-ng-if.component.ts

      @Component({
        selector: &#39;app-test-ng-if&#39;,
        templateUrl: &#39;./test-ng-if.component.html&#39;,
        styleUrls: [&#39;./test-ng-if.component.css&#39;]
      })
      export class TestNgIfComponent implements OnInit {
      
        isShow = true;
      
        constructor() { }
      
        ngOnInit() {
        }
      }
    • test- ng-if.component.html
      <div class="panel panel-primary">
        <div class="panel-heading">*ngIf的用法</div>
        <div class="panel-body">
          <p *ngIf="isShow" style="background-color:#ff3300">显示内容</p>
        </div>
      </div>
    • *
    • ngFor

    test-ng-for.component.ts

      @Component({
        selector: &#39;app-test-ng-for&#39;,
        templateUrl: &#39;./test-ng-for.component.html&#39;,
        styleUrls: [&#39;./test-ng-for.component.css&#39;]
      })
      export class TestNgForComponent implements OnInit {
      
        races = [
          {name: &#39;star&#39;},
          {name: &#39;kevin&#39;},
          {name: &#39;kent&#39;}
        ];
      
        constructor() { }
      
        ngOnInit() {
        }
      
      }
    • test-ng-for.component.html
      <div class="panel panel-primary">
        <div class="panel-heading">*ngFor用法</div>
        <div class="panel-body">
          <h3 id="名字列表">名字列表</h3>
          <ul>
            <li *ngFor="let name of names;let i=index;">
             {{i}}-{{name.name}}
            </li>
          </ul>
        </div>
      </div>
    • #ngSwitch

    test-ng-switch.component.ts

      @Component({
        selector: &#39;app-test-ng-switch&#39;,
        templateUrl: &#39;./test-ng-switch.component.html&#39;,
        styleUrls: [&#39;./test-ng-switch.component.css&#39;]
      })
      export class TestNgSwitchComponent implements OnInit {
      
        status = 1;
      
        constructor() { }
      
        ngOnInit() {
        }
      
      }
    • test-ng-switch.component.html
      #
      <div class="panel panel-primary">
        <div class="panel-heading">ngSwitch用法</div>
        <div class="panel-body">
          <div [ngSwitch]="status">
            <p *ngSwitchCase="0">Good</p>
            <p *ngSwitchCase="1">Bad</p>
            <p *ngSwitchDefault>Exception</p>
          </div>
        </div>
      </div>
    • 內建屬性型指令

    HTML 屬性與DOM 屬性關係

    少量的HTML 屬性與DOM 屬性之間有著一對一的對應關係, 如id;

      有些HTML 屬性沒有對應的DOM 屬性, 如colspan;
    • 有些DOM 屬性沒有對應的HTML 屬性, 如textContent;
    • 就算名字相同, HTML 屬性和DOM 屬性也不是同一樣東西;
    • HTML 屬性的值指定了初始值, DOM 屬性的值表示目前值; HTML 屬性的值無法改變, DOM 屬性的值可以改變。
    • 模板綁定是透過 DOM 屬性和事件來工作的, 而不是 HTML 屬性。
    • 注意:
    插值表達式與屬性綁定是同一個東西, 插值表達式屬於 DOM 屬性綁定。

    NgClass

    test-ng-class.component.ts

      @Component({
        selector: &#39;app-test-ng-class&#39;,
        templateUrl: &#39;./test-ng-class.component.html&#39;,
        styleUrls: [&#39;./test-ng-class.component.scss&#39;]
      })
      export class TestNgClassComponent implements OnInit {
        public currentClasses: {};
      
        public canSave = true;
        public isUnchanged = true;
        public isSpecial = true;
      
        constructor() { }
      
        ngOnInit() {
          this.currentClasses = {
            &#39;saveable&#39;: this.canSave,
            &#39;modified&#39;: this.isUnchanged,
            &#39;special&#39;: this.isSpecial
          };
        }
      }
    • test-ng-class. component.html
      <div class="panel panel-primary">
        <div class="panel-heading">NgClass用法</div>
        <div class="panel-body">
          <div [ngClass]="currentClasses">设置多个样式</div>
          <div [class.modified]=&#39;true&#39;></div>
        </div>
      </div>
    • test-ng-class.component.less
      .saveable {
          font-size: 18px;
      }
      
      .modified {
          font-weight: bold;
      }
      
      .special {
          background-color: #ff3300;
      }
    • NgStyle

    #test-ng-style.component.ts

      @Component({
        selector: &#39;app-test-ng-style&#39;,
        templateUrl: &#39;./test-ng-style.component.html&#39;,
        styleUrls: [&#39;./test-ng-style.component.css&#39;]
      })
      export class TestNgStyleComponent implements OnInit {
      
        currentStyles: { };
        canSave = false;
        isUnchanged = false;
        isSpecial = false;
      
        constructor() { }
      
        ngOnInit() {
          this.currentStyles = {
            &#39;font-style&#39;: this.canSave ? &#39;italic&#39; : &#39;normal&#39;,
            &#39;font-weight&#39;: !this.isUnchanged ? &#39;bold&#39; : &#39;normal&#39;,
            &#39;font-size&#39;: this.isSpecial ? &#39;36px&#39; : &#39;12px&#39;
          };
        }
      
      }
    • test-ng-style.component.html
      <div class="panel panel-primary">
        <div class="panel-heading">NgStyle用法</div>
        <div class="panel-body">
          <div [ngStyle]="currentStyles">
            用NgStyle批量修改内联样式!
          </div>
          <div [style.font-size]="isSpecial? &#39;36px&#39; : &#39;12px&#39;"></div>
        </div>
      </div>
    • NgModel

    test-ng-model.component.ts

      @Component({
        selector: &#39;app-test-ng-model&#39;,
        templateUrl: &#39;./test-ng-model.component.html&#39;,
        styleUrls: [&#39;./test-ng-model.component.css&#39;]
      })
      export class TestNgModelComponent implements OnInit {
      
        name = &#39;kevin&#39;;
      
        constructor() { }
      
        ngOnInit() {
        }
      
      }
    • test-ng-model.component.html
      <div class="panel panel-primary">
          <div class="panel-heading">NgModel的用法</div>
          <div class="panel-body">
              <p class="text-danger">ngModel只能用在表单类的元素上面</p>
              <input type="text" name="name" [(ngModel)]="name">
          </div>
      </div>
    • 小工具

    管道

    Angular 內建的常用管道:

    uppercase 與lowercase

    • #uppercase 將字母轉換成大寫{
    {'aaa' | uppercase}}
    lowercase 將字母轉換成小寫{

    {'BBB' | lowercase}}

    #Date
    • {
    { birthday | date: 'yyyy-MM-dd HH:mm:ss'}}

    number
    • {
    { pi | number: '2.2-2'}}
    2.2-2: 表示是保留2 位元整數和2 位小數。

    2-2: 表示最少 2 位小數,最大 2 位小數。

    範例
    • test-pipe.component.ts
    • @Component({
        selector: &#39;app-test-pipe&#39;,
        templateUrl: &#39;./test-pipe.component.html&#39;,
        styleUrls: [&#39;./test-pipe.component.css&#39;]
      })
      export class TestPipeComponent implements OnInit {
      
        currentTime: Date = new Date();
        
        str = &#39;aaa&#39;;
      
        money = 34.567;
      
      
        constructor() {
        }
      
        ngOnInit() {
          window.setInterval(
            () => { this.currentTime = new Date() }
            , 1000);
        }
      }
    test-pipe.component.html

    <div class="panel panel-primary">
        <div class="panel-heading">管道的用法</div>
        <div class="panel-body">
          {{ currentTime | date:&#39;yyyy-MM-dd HH:mm:ss&#39; }}
        </div>
        <div class="panel-body">
          {{ str | uppercase }}
        </div>
        <div class="panel-body">
          {{ money | number: &#39;2.2-2&#39; }}
        </div>
    </div>

    非空斷言

    test-not-null-assert.component.ts

      @Component({
        selector: &#39;app-test-safe-nav&#39;,
        templateUrl: &#39;./test-not-null-assert.component.html&#39;,
        styleUrls: [&#39;./test-not-null-assert.component.css&#39;]
      })
      export class TestSafeNavComponent implements OnInit {
      
        public currentValue: any = null;
      
        constructor() { }
      
        ngOnInit() {
        }
      
      }
    • test-not-null-assert. component.html
      <div class="panel panel-primary">
        <div class="panel-heading">安全取值</div>
        <div class="panel-body">
          名字:{{currentValue?.name}}
        </div>
      </div>
    • 更多程式相關知識,可造訪:
    • 程式設計教學
    ! !

    以上是詳解Angular中的模板文法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

  • 陳述
    本文轉載於:csdn。如有侵權,請聯絡admin@php.cn刪除
    超越瀏覽器:現實世界中的JavaScript超越瀏覽器:現實世界中的JavaScriptApr 12, 2025 am 12:06 AM

    JavaScript在現實世界中的應用包括服務器端編程、移動應用開發和物聯網控制:1.通過Node.js實現服務器端編程,適用於高並發請求處理。 2.通過ReactNative進行移動應用開發,支持跨平台部署。 3.通過Johnny-Five庫用於物聯網設備控制,適用於硬件交互。

    使用Next.js(後端集成)構建多租戶SaaS應用程序使用Next.js(後端集成)構建多租戶SaaS應用程序Apr 11, 2025 am 08:23 AM

    我使用您的日常技術工具構建了功能性的多租戶SaaS應用程序(一個Edtech應用程序),您可以做同樣的事情。 首先,什麼是多租戶SaaS應用程序? 多租戶SaaS應用程序可讓您從唱歌中為多個客戶提供服務

    如何使用Next.js(前端集成)構建多租戶SaaS應用程序如何使用Next.js(前端集成)構建多租戶SaaS應用程序Apr 11, 2025 am 08:22 AM

    本文展示了與許可證確保的後端的前端集成,並使用Next.js構建功能性Edtech SaaS應用程序。 前端獲取用戶權限以控制UI的可見性並確保API要求遵守角色庫

    JavaScript:探索網絡語言的多功能性JavaScript:探索網絡語言的多功能性Apr 11, 2025 am 12:01 AM

    JavaScript是現代Web開發的核心語言,因其多樣性和靈活性而廣泛應用。 1)前端開發:通過DOM操作和現代框架(如React、Vue.js、Angular)構建動態網頁和單頁面應用。 2)服務器端開發:Node.js利用非阻塞I/O模型處理高並發和實時應用。 3)移動和桌面應用開發:通過ReactNative和Electron實現跨平台開發,提高開發效率。

    JavaScript的演變:當前的趨勢和未來前景JavaScript的演變:當前的趨勢和未來前景Apr 10, 2025 am 09:33 AM

    JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

    神秘的JavaScript:它的作用以及為什麼重要神秘的JavaScript:它的作用以及為什麼重要Apr 09, 2025 am 12:07 AM

    JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

    Python還是JavaScript更好?Python還是JavaScript更好?Apr 06, 2025 am 12:14 AM

    Python更适合数据科学和机器学习,JavaScript更适合前端和全栈开发。1.Python以简洁语法和丰富库生态著称,适用于数据分析和Web开发。2.JavaScript是前端开发核心,Node.js支持服务器端编程,适用于全栈开发。

    如何安裝JavaScript?如何安裝JavaScript?Apr 05, 2025 am 12:16 AM

    JavaScript不需要安裝,因為它已內置於現代瀏覽器中。你只需文本編輯器和瀏覽器即可開始使用。 1)在瀏覽器環境中,通過標籤嵌入HTML文件中運行。 2)在Node.js環境中,下載並安裝Node.js後,通過命令行運行JavaScript文件。

    See all articles

    熱AI工具

    Undresser.AI Undress

    Undresser.AI Undress

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

    AI Clothes Remover

    AI Clothes Remover

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

    Undress AI Tool

    Undress AI Tool

    免費脫衣圖片

    Clothoff.io

    Clothoff.io

    AI脫衣器

    AI Hentai Generator

    AI Hentai Generator

    免費產生 AI 無盡。

    熱門文章

    R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
    3 週前By尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O.最佳圖形設置
    3 週前By尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O.如果您聽不到任何人,如何修復音頻
    3 週前By尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25:如何解鎖Myrise中的所有內容
    4 週前By尊渡假赌尊渡假赌尊渡假赌

    熱工具

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    將Eclipse與SAP NetWeaver應用伺服器整合。

    記事本++7.3.1

    記事本++7.3.1

    好用且免費的程式碼編輯器

    Dreamweaver Mac版

    Dreamweaver Mac版

    視覺化網頁開發工具

    SublimeText3 Linux新版

    SublimeText3 Linux新版

    SublimeText3 Linux最新版