需要将值从<ng-content [value] =“ Hello World”> </ng-content>传递给父级

时间:2019-02-19 13:21:45

标签: javascript angular

尝试通过ng-content将值从子级传递给父级

父组件

 class Pump():
 //member variable
 account_holder
 balance_amount

   // constructor
   def __init__(self,ah,bal):
   |    self.account_holder = ah
   |    self.balance_amount = bal

   def getPumps(self):
   |    print("The details of your account are:"+self.account_number + self.balance_amount)

 //object = class(*passing values to constructor*)
 p = Pump("Tahir",12000)
 p.getPumps()

第1块组件

<block-1>
 <block-2 [value]="{{I want this value from child}}"></block-2>
</block-1>

1 个答案:

答案 0 :(得分:0)

结合使用EventEmitter@Output装饰器,将数据从子级传递到父级。

ChildComponent:

export class ChildComponent {
  @Output() str = new EventEmitter<string>();

  pass(str: string) {
    this.str.emit('Pass this string to parent');
  }
}

ParentComponent:

@Component({
  selector: 'app-parent',
  template: `
    <h2>Pass data?</h2>
    <app-child
      (str)="onPassed($event)">
    </app-child>
  `
})
export class ParentComponent {
  onPassed(str: string) {
    console.log(str);
  }
}

在循环pass()时触发事件(<div *ngFor="let value form values">)。