Search code examples
angularangular2-components

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.


Solution

  • 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>