天天看點

Angular input decorator學習筆記

Decorator that marks a class field as an input property and supplies configuration metadata. The input property is bound to a DOM property in the template. During change detection, Angular automatically updates the data property with the DOM property’s value.

參數名稱:The name of the DOM property to which the input property is bound.

看個例子:

Angular input decorator學習筆記

@Component({
  selector: 'bank-account',
  template: `
    Bank Name: {{bankName}}
    Account Id: {{id}}
  `
})
class BankAccount {
  // This property is bound using its original name.
  @Input() bankName: string;
  // this property value is bound to a different property name
  // when this component is instantiated in a template.
  @Input('account-id') id: string;

  // this property is not bound, and is not automatically updated by Angular
  normalizedBankName: string;
}

@Component({
  selector: 'app',
  template: `
    <bank-account bankName="RBC" account-id="4747"></bank-account>
  `
})
class App {}
      

子元件的模闆裡,顯示bankName和id兩個模型字段。

這兩個字段,加上了@Input裝飾器,表明其資料源,需要外部消費者傳入(通常是消費這個子元件的父元件

Angular input decorator學習筆記
Angular input decorator學習筆記
Angular input decorator學習筆記
Angular input decorator學習筆記
Angular input decorator學習筆記

繼續閱讀