简体   繁体   English

在父组件中单击按钮时如何更改子组件的@input值(子组件位于* ngfor循环中)

[英]How to change the @input value of child component when button click in parent component ( child component is in *ngfor loop )

I want to change the input value of discount when i click the button. 单击按钮时,我想更改折扣的输入值。

//parent.component.html
<button mat-raised-button (click)="addDiscount(5)" >5</button>
<div *ngFor="let items of itemSelected">
    <item-box [quantity]="quantity" [discount]="discount">
     </item-box>
 </div>



 //parent.component.ts
    discount: number = 0;

     addDiscount(disc: number){
       this.discount = disc;
      }

From your code it seems changing parent property will pass changed value to each child component 从您的代码看来,更改父属性会将更改后的值传递给每个子组件

<button (click)="addDiscount(5)">Click Me to Send Data to Child</button>
 <div *ngFor="let item of items" >
  <br>
  <app-child [discount]="discount" [childToMaster]=master 
  (childToParent)="childToParent($event)">
 </app-child>
</div>

Parent-Child-Interaction Code Demo showing changing parent property is accessible in all child components 在所有子组件中都可以访问显示更改父属性的“父子交互代码演示”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM