簡體   English   中英

如何在我的自定義角度組件上設置屬性

[英]How to set an attribute on my custom angular component

我是Angular的新手,正在嘗試創建我的第一個UI。

我創建了一個自定義組件app-dropdown,它基本上是Material選擇元素。 (貨幣數組位於組件的.ts文件中)

<mat-form-field>
  <mat-select placeholder="Currency" >
    <mat-option *ngFor="let currency of currencies" [value]="currency">
      {{currency}}
    </mat-option>
  </mat-select>
</mat-form-field>

然后,我將該組件放置在另一個自定義組件app-buy-card中。

<mat-card>
  <p>I have</p><app-dropdown></app-dropdown><br>
  <p>I want</p><app-dropdown></app-dropdown><br>
  <p>I live in</p><app-dropdown></app-dropdown><br>
</mat-card>

當在app-buy-card中定義app-dropdown組件時,我想向開始的app-dropdown標簽添加一個屬性,指定默認情況下應選擇哪些元素。 我已經嘗試使用綁定和雙向綁定以及ng-init來實現這一點,但是我無法使其正常工作。 有任何想法嗎?

您正在尋找@Input

@Input selectedValue: string;

ngOnInit() {
    // here you can access the selectedValue input
}

然后在組件上使用它:

<app-dropdown [selectedValue]="usd">
</app-dropdown>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM