需要访问属性子元素。上级:
<div>
<shipment-detail #myCarousel ></shipment-detail>
</div>
@Component({
selector: "testProject",
templateUrl: "app/partials/Main.html",
directives: [ShipmentDetail] })
class AppComponent {
getChildrenProperty() {
// I want to get access to "shipment"
}
}
儿童:
@Component({
selector: "shipment-detail",
})
export class ShipmentDetail {
shipment: Shipment;
}
请参阅组件交互操作手册。因此,使用@ViewChild()并向ShipmentDetail添加一个方法,父母可以调用该方法来检索装运值,或者直接访问该属性,如下所示(因为我很懒,并且不想编写API /方法):
@Component({
selector: "testProject",
templateUrl: "app/partials/Main.html",
directives: [ShipmentDetail]
})
export class AppComponent {
@ViewChild(ShipmentDetail) shipmentDetail:ShipmentDetail;
ngAfterViewInit() {
this.getChildProperty();
}
getChildProperty() {
console.log(this.shipmentDetail.shipment);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句