Reputation: 726
import {Component, Input} from '@angular/core';
@Component({
selector: 'aricle-root',
template: '<li>{{article}}</li>'
})
export class Article {
@Input() article: object;
constructor() {
console.log(this.article); //undefined
}
}
So when i pass the variable {{artical}}
from my parent component to this one and it recieves it and renders it ok, but what if i want to do something with that variable like print it on the console for an example. It seems to be always undefined
Upvotes: 1
Views: 34
Reputation: 437
In the constructor your variable is not set yet. You can access when setted :
import {Component, Input, OnChanges} from '@angular/core';
@Component({
selector: 'aricle-root',
template: '<li>{{article}}</li>'
})
export class Article implements OnChanges {
@Input() article: object;
constructor() {
}
ngOnChanges(model){
console.log(article)
}
}
Upvotes: 1
Reputation: 136144
Seems like you want to get an intimation when your Input
binding changes. You could use ngOnChanges
hook to keep eye on Input
property changes, and place the code in it.
//import { SimpleChanges, SimpleChanges } from '@angular/core';
ngOnChanges(changes: SimpleChanges){
console.log(changes)
}
Upvotes: 1