[英]How to Pass Data or values From One Component to another in Angular
Good day Community, Please i am working on an ecommerce project and i would like to pass the quantity entered in my cart component to my checkout component, and when the value of the quantity changes, i would like it to change in the checkout component also.美好的一天社区,请我正在做一个电子商务项目,我想将在我的购物车组件中输入的数量传递给我的结帐组件,当数量的值发生变化时,我希望它在结帐组件中也发生变化. I have searched for solutions but couldnt find any.
我已经搜索了解决方案,但找不到任何解决方案。 Someone please assist me.
有人请帮助我。 Thank you
谢谢
This is my cart.component.ts这是我的 cart.component.ts
trackByCartItems(index: number, item: any) {
return item._id;
}
get cartItems() {
return this.data.getCart();
}
get cartTotal() {
let total = 0;
this.cartItems.forEach((data, index) => {
total += data['price'] * this.quantities[index];
});
return total;
}
removeProduct(index, product) {
this.quantities.splice(index, 1);
this.data.removeFromCart(product);
}
and my cart.component.html和我的购物车.component.html
<div class="col-1 mt-5 mt-md-0 p-0 induc">
<input type="number" class="form-control text-left boy" [(ngModel)]="cart.quantities[i]">
</div>
Thank you in advance先感谢您
You have a few options, as detailed in this link.您有几个选项,如此链接中所述。
You can use @input
, if the relationship between the components is parent-child, or you can use a service.如果组件之间的关系是父子关系,则可以使用
@input
,也可以使用服务。
I recommend reading up more on passing data between components, in this link.我建议在此链接中阅读有关在组件之间传递数据的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.