需要访问属性子元素。
起源:

<div>
   <shipment-detail #myCarousel ></shipment-detail>
</div>

@Component({
  selector: "testProject",
  templateUrl: "app/partials/Main.html")
class AppComponent {
  getChildrenProperty() {
  // I want to get access to "shipment"
  }
}

儿童:
@Component({
      selector: "shipment-detail",
    })
    export class ShipmentDetail  {
      shipment: Shipment;
    }

最佳答案

@ViewChild@ViewChildren修饰符提供对子组件类的访问:

@Component({
    selector: "testProject",
    templateUrl: "app/partials/Main.html")
class AppComponent {

    @ViewChild(ShipmentDetail) ShipDetails: ShipmentDetail;

    getChildrenProperty() {
        console.log(this.ShipDetails.shipment);
    }
}

@ViewChild需要子组件类的名称作为其输入,并在父组件中找到其选择器。
在您的情况下,应该是ShipmentDetail

关于angular - angular 2从父组件类访问子组件对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45761976/

10-09 21:14