@input is useful to bind data between components
First, import it in your component
import { Input } from '@angular/core';
Then, add the input as a property of your component class
@Input() car: any;
Let's say that the selector of your component is 'car-component', when you call the component, add the attribute 'car'
<car-component [car]="car"></car-component>
Now your car is accessible as an attribute in your object (this.car)
Full Example :
export class CarEntity {
constructor(public brand : string, public color : string) {
}
}
import { Component, Input } from '@angular/core';
import {CarEntity} from "./car.entity";
@Component({
selector: 'car-component',
template: require('./templates/car.html'),
})
export class CarComponent {
@Input() car: CarEntity;
constructor() {
console.log('gros');
}
}
import { Component } from '@angular/core';
import {CarEntity} from "./car.entity";
import {CarComponent} from "./car.component";
@Component({
selector: 'garage',
template: require('./templates/garage.html'),
directives: [CarComponent]
})
export class GarageComponent {
public cars : Array<CarEntity>;
constructor() {
var carOne : CarEntity = new CarEntity('renault', 'blue');
var carTwo : CarEntity = new CarEntity('fiat', 'green');
var carThree : CarEntity = new CarEntity('citroen', 'yellow');
this.cars = [carOne, carTwo, carThree];
}
}
<div *ngFor="let car of cars">
<car-component [car]="car"></car-component>
</div>
<div>
<span>{{ car.brand }}</span> |
<span>{{ car.color }}</span>
</div>