Get Data in Component TS


#1

Hi,

I’ve tried to follow the Angular Documentation, but I can’t seem to pass data to my component type script file.

There is my TS component :

import {Component, Input} from '@angular/core';
import {PollProvider} from "../../providers/poll/poll";
import {NavParams} from "ionic-angular";

@Component({
  selector: 'poll-box',
  templateUrl: 'poll-box.html',
})
export class PollBoxComponent {

  private _poll: any;
  public pollData: any;

  @Input()
  set poll(poll: any) {
    this._poll = poll ? poll : null;
    this.pollData = poll;
  }
  get poll(): any {
    return this._poll;
  }
  //@Input() poll: any;
  @Input() auth: boolean;
  @Input() index: number;
  @Input() userId: number;

  public responses: any;

   constructor(public pollProvider: PollProvider, public navParams: NavParams) {

    console.log(this._poll);
    console.log(this.poll);
    console.log(this.pollData);
    console.log(navParams.get('poll'));
  }
}

And My Html page where I call the component :

<div class="article-poll">
      <poll-box [poll]="poll" [auth]="auth" [userId]="user ? user.id : null" [index]="i" *ngFor="let poll of polls; let i = index"></poll-box>
    </div>

All of my console.log return “undefined” but correctly appear in html component. So, what am I doing wrong or what I did not grasp ?

If anybody can help =)

Thanks you a lot !


#2

I’m going to guess “timing”. You can’t rely on input properties having received bindings until ngOnChanges() is called.


#3

Okay !

So I need to use setTimeOut function to get data thanks !

But how to use ngOnChange in Ionic Component ? Is there a cleaner way to do that ?

Thanks.