Все Едно
Все Едно

Reputation: 726

Working with data from parent component

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

Answers (2)

Yesub
Yesub

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

Pankaj Parkar
Pankaj Parkar

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

Related Questions