Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Pass variables to constructor in html

I am creating a component to be reused throughout my app, the important code below

.ts

export class DieInputComponent {
  constructor(private sides: Number, private dice: Number) { 
  }
}

.html

<input type="number" placeholder="{{dice}}d{{sides}}"/>

I am creating these objects in my html templates like so

<die-input></die-input>

How would I go about inputting the constructor variables in a html tag? For the life of me I can't find an appropriate reference on how to do it.

like image 944
Crimthann Avatar asked Jul 16 '26 03:07

Crimthann


1 Answers

If you need some value to be available inside your component class then simply use @Input variable and pass the value as attribute of your component

export class DieInputComponent{    
    @Input()sides:Number;
    @Input()dice:Number;
 }   
    <die-input [sides]='6' [dice]='1'></die-input>
like image 176
Ravinder Kumar Avatar answered Jul 17 '26 20:07

Ravinder Kumar



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!