目次
補間式
テンプレート変数
値バインディング: []
# # HTML 属性と DOM 属性の間の関係
パイプライン
ホームページ ウェブフロントエンド 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>2+2 = {{2 + 2}}</h3>
    <h3>调用方法{{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" />
      </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 class="panel panel-primary">
        <div class="panel-heading">事件绑定</div>
        <div class="panel-body">
            <button class="btn btn-success" (click)="btnClick($event)">点击按钮</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>名字列表</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 など) の間には 1 対 1 のマッピング関係があります;

一部の HTML 属性には、colspan などの DOM 属性がありません;
  • 一部の DOM 属性には、textContent などの対応する HTML 属性がありません;
  • 名前が同じであっても、HTML 属性とDOM 属性は同じものではありません。
  • HTML 属性の値は初期値を指定し、DOM 属性の値は現在の値を示します。HTML 属性の値は変更できません。 DOM属性の変更が可能です。
  • テンプレート バインディングは、HTML 属性ではなく、DOM プロパティとイベントを通じて機能します。
  • 注:
  • 補間式と属性バインディングは同じものであり、補間式は 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の組み込み共通パイプ:

大文字と小文字

    大文字 文字を大文字に変換します {
  • {'aaa' | 大文字}}
  • 小文字 文字を小文字に変換します {
{'BBB' | 小文字}}

##日付

##{
{ 誕生日 | 日付: 'yyyy-MM-dd HH:mm:ss'}}
  • #number

{

{ pi |number: '2.2-2'}}
2.2-2: 2 桁の整数と 2 桁の整数を示します数値は予約された 10 進数です。
    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>
ログイン後にコピー
  • 非 null アサーション

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 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Angular のメタデータとデコレータについて話しましょう Angular のメタデータとデコレータについて話しましょう Feb 28, 2022 am 11:10 AM

この記事では、Angular の学習を継続し、Angular のメタデータとデコレータを理解し、それらの使用法を簡単に理解します。

Ubuntu 24.04 に Angular をインストールする方法 Ubuntu 24.04 に Angular をインストールする方法 Mar 23, 2024 pm 12:20 PM

Angular.js は、動的アプリケーションを作成するための無料でアクセスできる JavaScript プラットフォームです。 HTML の構文をテンプレート言語として拡張することで、アプリケーションのさまざまな側面を迅速かつ明確に表現できます。 Angular.js は、コードの作成、更新、テストに役立つさまざまなツールを提供します。さらに、ルーティングやフォーム管理などの多くの機能も提供します。このガイドでは、Ubuntu24 に Angular をインストールする方法について説明します。まず、Node.js をインストールする必要があります。 Node.js は、ChromeV8 エンジンに基づく JavaScript 実行環境で、サーバー側で JavaScript コードを実行できます。ウブにいるために

Angular Learning State Manager NgRx の詳細な説明 Angular Learning State Manager NgRx の詳細な説明 May 25, 2022 am 11:01 AM

この記事では、Angular のステートマネージャー NgRx について深く理解し、NgRx の使用方法を紹介します。

Angular のサーバーサイド レンダリング (SSR) について説明する記事 Angular のサーバーサイド レンダリング (SSR) について説明する記事 Dec 27, 2022 pm 07:24 PM

Angular Universal をご存知ですか?これは、Web サイトがより優れた SEO サポートを提供するのに役立ちます。

Angular で Monaco エディターを使用する方法の簡単な分析 Angular で Monaco エディターを使用する方法の簡単な分析 Oct 17, 2022 pm 08:04 PM

Angularでモナコエディタを使用するにはどうすればよいですか?以下の記事は、最近業務で使用したangularでのmonaco-editorの使い方を記録したものですので、皆様のお役に立てれば幸いです。

Angular + NG-ZORRO でバックエンド システムを迅速に開発 Angular + NG-ZORRO でバックエンド システムを迅速に開発 Apr 21, 2022 am 10:45 AM

この記事では、Angular の実践的な経験を共有し、angualr と ng-zorro を組み合わせてバックエンド システムを迅速に開発する方法を学びます。

フロントエンド開発に PHP と Angular を使用する方法 フロントエンド開発に PHP と Angular を使用する方法 May 11, 2023 pm 04:04 PM

インターネットの急速な発展に伴い、フロントエンド開発テクノロジーも常に改善され、反復されています。 PHP と Angular は、フロントエンド開発で広く使用されている 2 つのテクノロジーです。 PHP は、フォームの処理、動的ページの生成、アクセス許可の管理などのタスクを処理できるサーバー側スクリプト言語です。 Angular は、単一ページ アプリケーションの開発やコンポーネント化された Web アプリケーションの構築に使用できる JavaScript フレームワークです。この記事では、PHPとAngularをフロントエンド開発に使用する方法と、それらを組み合わせる方法を紹介します。

Angular の独立したコンポーネントの簡単な分析とその使用方法を説明します。 Angular の独立したコンポーネントの簡単な分析とその使用方法を説明します。 Jun 23, 2022 pm 03:49 PM

この記事では、Angular の独立コンポーネント、Angular で独立コンポーネントを作成する方法、および既存のモジュールを独立コンポーネントにインポートする方法について説明します。

See all articles