我想将数据从parent
控制器传递到child
控制器,当用户click
在元素上存在parent
时。
例如
<div (click)="PassDataToChild(orderNo)"></div>
.TS
PassDataToChild(orderNo){
//Got the orderNo
//How to send it to the child component, and let the child read it?
}
答案 0 :(得分:-1)
您可以使用@Input()
装饰器将数据从Parent传递给Child。
代码附在
下面import {Component, Input} from '@angular/core';
@Component({
selector: 'child-comp',
template: '<div>{{orderNo}}</div>'
})
export class Child {
@Input() orderNo: string;
}
import {Component} from '@angular/core';
@Component({
selector: 'parent-comp',
template: '<div (click)="PassDataToChild()"></div> <child-comp [orderNo]="orderNo"></child-comp>'
})
export class Parent {
orderNo: string;
PassDataToChild() {
this.orderNo = 'Dummy Order';
}
}