简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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