Angular input decorator学习笔记
2023-09-14 09:04:02 时间
https://angular.io/api/core/Input
Input decorator只能用在Angular class字段里, 用于指定元数据,将class字段指定成input property.
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.
看个例子:
看个例子:
@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装饰器,表明其数据源,需要外部消费者传入(通常是消费这个子组件的父组件)
父组件的传值方式:
测试截图:
加了中括号后,表明等号右边的是一个表达式,不能当成普通字符串对待。
做个实验,如果删除子Component里的@Input:
最后什么也无法显示出来:
最后效果:
parent:
加上中括号之后,需要传入一个 Component 属性的路径。
结果:
更多Jerry的原创文章,尽在:“汪子熙”:
相关文章
- node、npm 、package.json、Angular Cli、webpack之间的关系(Windows环境下)
- 关于 Angular 里 module 和 Component 包含粒度的一个讨论
- Angular和Vue.js 深度对比
- vue相比jquery_angular和vue哪个厉害
- uat环境和生产环境的区别_angular 生产环境 相对路径无效
- angular框架发展史
- Angular 模块封装概念常见的错误理解
- Angular forRoot 方法的使用场合介绍
- 关于 Angular 工程文件 angular.json 里的 inlineCritical 属性
- 使用 Angular HTTP_INTERCEPTOR 拦截器来记录超时请求的一些思考
- Angular 项目中 angular.json builder 字段的可选项介绍
- 备受 Vue、Angular 和 React 青睐的 Signals 演进史
- MySQL Error number: 3739; Symbol: ER_SRS_INVALID_ANGULAR_UNIT; SQLSTATE: SR002 报错 故障修复 远程处理
- Web 前端框架:Angular 4.0.0 正式版发布
- Angular 框架将进入 2.0 时代