0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Angular】バインディングのパターン

0
Posted at

結論

  • プロパティバインディング→「右側の式を評価して、左側のプロパティに値を渡す」
  • そのプロパティが、HTML要素のものなのか、ディレクティブのものなのか、コンポーネントのものなのかを区別する必要がある

詳細

パターン①:HTML要素のプロパティに渡す

<input [type]="inputType">
inputType = 'password';

input 要素がもともと持っている type プロパティに値を渡している。

パターン②:ディレクティブのプロパティに渡す

<input [ngClass]="{ 'error': hasError }">
hasError = true;
  • [ngClass]:NgClass ディレクティブの入力プロパティ(@input()で外部から値を受け取れるようにしたプロパティ)に値を渡す
  • { 'error': hasError }:渡す値を作る式
  • hasError が true:error クラスを適用する
    つまり、今度はHTML要素がもともと持っているプロパティではなく、ディレクティブが提供する入力プロパティに値を渡す

パターン③:Angularコンポーネントのプロパティに渡す

<app-user-card [user]="currentUser"></app-user-card>

app-user-card は独自に作ったAngularコンポーネントだとする

@Input() user!: User;
  • この user は、コンポーネントが外部から値を受け取るために用意した入力プロパティ
  • currentUser の値を、app-user-card コンポーネントの user に渡しているよ

区別

  • <input> や <button> など → HTML要素
  • <app-user-card> など → Angularコンポーネントの可能性が高い
  • <input> に [ngClass] などが付いている → ディレクティブの可能性がある
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?