Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Input angular 2 pass number or object variables

It is possible to send number or a whole class instance to @Input ? I am trying to send the value like this value="id" or this [value]="id". for example id = 1.But in console i see that my object received string value id and not the value of id, those = 1. In console i see this:

 id :  id
    typeof:  string

UPDATED

My class that store value:

private id: number ;

  show(id: number): void { 
    this.id = id;
  }

In html page of my component i assign to selector of my childComponent value like this value="id" or this [value]="id".

And my child class is very simple:

 @Input() value: number;
  constructor() { 
  }
   
   ngOnChanges() {
     console.log(`ngOnChanges: `, this.value);
   }
like image 263
iliya.rudberg Avatar asked Jan 01 '26 15:01

iliya.rudberg


1 Answers

It is possible to send number or a whole class instance to @Input ?

Yes, it is possible.

Notes:

  1. For Number and boolean variables you can pass them around as [value]="12" and [value]="true".
  2. For passing strings, you need to enclose the string within single quotes to pass them as it is. [value]="'Justastring'".
  3. For object or variables defined in the component, you can simply pass them.

For @Input() bound attributes, the common way is [value]="id".

Angular will now simply look for variable named id in the component's class and assign the value to the value variable in the child component.

like image 136
rootkill Avatar answered Jan 03 '26 04:01

rootkill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!