html - 如何从属性绑定到组件角度获取值
问题描述
我正在使用角度,这是我的问题:
我在名为customers.component.html的父视图中调用一个组件:
<div id="listas" class="listas">
<div class="fila_lista" *ngFor="let customer of customers">
<app-details-customer [customer]="customer"></app-details-customer>
</div>
</div>
如您所见,我正在使用属性绑定将customer
变量发送到子组件...
我的问题是,当我收到customer
子组件中的变量时,如何获取child.component.ts
文件中的值?不在子组件的视图中,只是在child.component.ts
像这样的东西:
export class ChildComponent implements OnInit {
@Input() customer: any;
value: any;
constructor(){
this.getValue();
}
getValue() {
value = customer.value
}
有办法做到这一点吗?因为如果我尝试这样做,它不起作用......
谢谢!
解决方案
据我了解这个问题,您可以使用 setter 函数以及 OnInit 生命周期挂钩函数来实现这一点。
使用二传手:
将您的 @Input() 客户替换为:
@Input() set customer(customerr) {
this.value = customerr.value;
}
value: any;
使用 onInit:
export class ChildComponent implements OnInit {
@Input() customer: any;
value: any;
ngOnInit() {
this.value = this.customer.value;
}
}
随时再问:)
推荐阅读
- reactjs - 如何使用 react-native-testing-library getByRole
- bash - 是否可以使用在循环中更改其名称的变量作为 shell 脚本中函数的输入?
- reactjs - 如何通过反应原生导航传递参数
- jquery - 如何正确地将 JSON 发送到谷歌函数?
- ruby-on-rails - 使用公寓 gem 为 API 编写 RSpec 测试
- asp.net-mvc - 将 ContentBuilder.js 与 .NET MVC 一起使用
- python - 使用 MFRC522 和 RPi 读取 RFID 标签
- python - 为多列创建虚拟特征
- c++ - 迭代深化深度优先搜索启发式解决“3 Missionary And Cannibal”挑战
- javascript - TypeError:无法读取未定义的属性“国家”