Angular @Input裝飾器 表明為輸入屬性


小白一只,最近在看Angular的東西,記錄備忘。
 ==========手====動====分====割====線============
 

1.建立一個子模塊fathertochild.ts

 1 import { Component, Input } from '@angular/core';
 2 
 3 @Component({
 4   selector: 'page-myinput',
 5   template: `<h1>{{title}}{{subtitle}}</h1>
 6   Bank Name: {{bankName}} &nbsp;&nbsp;&nbsp;
 7   Account Id: {{id}}`
 8 })
 9 
10 export class MyInputComponent {
11   @Input() subtitle='';
12   title='This is Title.';
13   @Input('account-id') id:string;//account-id為id的別名,不要也沒關系
14   @Input()bankName:string;
15 }
16  

2.別忘了在app.module.ts中聲明:

1 import { MyInputComponent } from "../pages/myinput";
2  
3 @NgModule({
4   declarations: [
5     ……
6     MyInputComponent
7   ],
8  

 

3.在父模塊中使用:

 
1 @Component({
2   selector: 'page-home',
3   template:`
4   <page-myinput bank-name="RBC" account-id="4747" subtitle="this is subtitle"><page-myinput>`,
5   })
6 export class HomePage {
7   ……
8   }

 

 
最終效果:
Bank Name沒有值是因為使用了不存在的名字:bank-name,改成bankName就好了。
 

參考資料:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM