crystal_test
crystal_test

Reputation: 691

angular 2 Access child component object from parent component class

Need to access to property children element.

Parent:

<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" 
  }
}

Children:

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

Upvotes: 3

Views: 2240

Answers (1)

RIYAJ KHAN
RIYAJ KHAN

Reputation: 15292

The @ViewChild and @ViewChildren decorators provide access to the class of child component:

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

    @ViewChild(ShipmentDetail) ShipDetails: ShipmentDetail;

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

@ViewChild requires the name of child component class as its input and finds its selector in the parent component.

In your case it should be ShipmentDetail.

Upvotes: 4

Related Questions