Im currently learning angular2 and the ang-book2 suggest that to pass a variable to a child component i should:
In parent view (random_number defined in Parent app.ts):
<app-child
[passedToChild] = "random_number"
></app-child>
In child component:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css'],
inputs: ['passedToChild']
})
export class ChildComponent implements OnInit {
passedToChild :number;
constructor() { console.log(this.passedToChild)}
ngOnInit() {
}
}
This results in undefined output in console.
On the other hand here https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-child they use @Input to pass a var to the child element.
The second option seems to work for me but I want to understand why. And neither book nor the page above explains the difference or the reason why the first method results in undefined.