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