javascript - @Input() property in angular component returns empty array - Stack Overflow

I have calendar ponent with data property decorated as @Input():import { Component, OnInit, Input, OnCh

I have calendar ponent with data property decorated as @Input():

import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core';

@Component({
  selector: 'app-calendar',
  templateUrl: './calendarponent.html',
  styleUrls: ['./calendarponent.css']
})
export class CalendarComponent implements OnInit, OnChanges {
  @Input() data: CalendarDay[];

  constructor() {
    this.data = [];
  }

  ngOnInit() {
    this.initDays();
  }

  ngOnChanges(changes: SimpleChanges) {
    console.log(this.data);
    console.log(changes.data);
  }
}

I pass data in from another ponent like that:

<app-calendar [data]="this.calendarData"></app-calendar>

And passed data gets rendered by *ngFor in the calendar ponent (it renders perfectly and everything works just fine):

<div *ngFor="let item of data">{{item.date}}</div>

I want to parse this data first before rendering it into view and whenever i try to console.log data property within the calendar ponent i get strange array, its shows as empty, i can 'open' it from browser console:

.

And when i try to log value like that:

console.log(this.data[0])

or

console.log(changes.data.currentValue[0])

i get undefined value.

I have calendar ponent with data property decorated as @Input():

import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core';

@Component({
  selector: 'app-calendar',
  templateUrl: './calendar.ponent.html',
  styleUrls: ['./calendar.ponent.css']
})
export class CalendarComponent implements OnInit, OnChanges {
  @Input() data: CalendarDay[];

  constructor() {
    this.data = [];
  }

  ngOnInit() {
    this.initDays();
  }

  ngOnChanges(changes: SimpleChanges) {
    console.log(this.data);
    console.log(changes.data);
  }
}

I pass data in from another ponent like that:

<app-calendar [data]="this.calendarData"></app-calendar>

And passed data gets rendered by *ngFor in the calendar ponent (it renders perfectly and everything works just fine):

<div *ngFor="let item of data">{{item.date}}</div>

I want to parse this data first before rendering it into view and whenever i try to console.log data property within the calendar ponent i get strange array, its shows as empty, i can 'open' it from browser console:

.

And when i try to log value like that:

console.log(this.data[0])

or

console.log(changes.data.currentValue[0])

i get undefined value.

Share Improve this question edited May 30, 2019 at 11:16 Manish Balodia 1,8772 gold badges24 silver badges38 bronze badges asked May 29, 2019 at 19:25 RintalesRintales 451 silver badge9 bronze badges 2
  • 2 Have you tried removing the initialisation from the constructor? That doesn't need to be there. You should avoid putting anything other than dependency injection in the constructor. – Will Alexander Commented May 29, 2019 at 19:28
  • Please try to create an minimal reproducible example, I remend stackblitz. – Igor Commented May 29, 2019 at 19:30
Add a ment  | 

1 Answer 1

Reset to default 6

Delete this.data = [] from constructor, avoid change anything when you use dependecy injecton.

And use set and get for each Input() that you want to use in your template, it's a best practice.

  private _data: CalendarDay[];

  @Input() set data(data: CalendarDay[]) {
    if(data) {
     this._data = data;
    }
  }

  get data(): CalendarDay[] {
    return this._data;
  }

And in your HTML you should pass it with:

<app-calendar [data]="calendarData"></app-calendar>

In calendar ponent you can use with

<div *ngFor="let item of data">{{item.date}}</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745318056a4622310.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信