Luis Bermúdez
Luis Bermúdez

Reputation: 654

How to get the value from a property binding to a component angular

i am using angular and this is my question:

I am calling a component in a parent view called customers.component.html:

<div id="listas" class="listas">
    <div class="fila_lista" *ngFor="let customer of customers">
        <app-details-customer [customer]="customer"></app-details-customer>
    </div>
</div>

As you can see, i am using a property binding sending the customer variable to the child component...

my question is, when i receive the variable customer in the child component, how can i get the value in the child.component.ts file? Not in the view of the child component, just in the child.component.ts

Something like this:

export class ChildComponent implements OnInit {
   @Input() customer: any;
   value: any;

   constructor(){
     this.getValue();
   }

    getValue() {
      value = customer.value
   }

There is a way to do that? Because if i try that, it don´t work...

Thank you!

Upvotes: 0

Views: 811

Answers (1)

Jas
Jas

Reputation: 326

as far as i understand this question you can achieve this using a setter function as well as OnInit lifecycle hook function.

Using Setter:

Replace your @Input() customer with :

@Input() set customer(customerr) {
   this.value = customerr.value;

}
value: any;

Using onInit:

export class ChildComponent implements OnInit {
   
   @Input() customer: any;
   value: any;
   
   ngOnInit() {
     this.value = this.customer.value;
   }
}

Feel free to ask again :)

Upvotes: 1

Related Questions