1

I'm trying to pass json value using Input decorator but its not working. I tried to pass data parent component to child component but its not working properly.

Child Component

<div class="card">
  <div class="card-body">
    <h4 class="card-title">{{ analyticsTitle }}</h4>
      <div *ngFor="let data of datas">
        <a href="{{data.redirectionUrl}}" class="card-link">{{data.description}}</a>
      </div>
      <div class="text-right view-more">
        <a href="{{viewMoreUrl}}">
            {{ viewMore }}
        </a></div>
    </div>
</div>

TS

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


@Component({
  selector: 'app-widget',
  templateUrl: './widget.component.html',
  styleUrls: ['./widget.component.scss']
})
export class WidgetComponent implements OnInit {
  @Input('joke') datas=[];

  ngOnInit() {
  }
}

Parent Component

<app-widget *ngFor="let j of analytics" [joke]="j"></app-widget>

TS

import { Component, OnInit } from '@angular/core';
import { HomeService } from "./../../service/home.service";

@Component({
  selector: 'app-bi-analytics-platform',
  templateUrl: './bi-analytics-platform.component.html',
  styleUrls: ['./bi-analytics-platform.component.scss']
})
export class BiAnalyticsPlatformComponent implements OnInit {


  analytics = [];
  analyticsTitle = '';
  viewMore = '';
  viewMoreUrl = '';
  constructor(private homeService: HomeService) { 
   }

   ngOnInit() {

    this.homeService.homeDataWidget().subscribe(response => {
      let res = response[0];
      let heading = res['BI-Analytics'][0]['heading'][0];
      this.analytics = res['BI-Analytics'][0]['body'];
      this.analyticsTitle = heading['title'];
      this.viewMore = heading['viewmore'];
      this.viewMoreUrl = heading['viewmoreURL'];
    });

   }

}

JSON 1

[
  {
  "BI-Analytics": [{
    "heading": [{
      "viewmore": "View More",
      "viewmoreURL": "http://google.com"
    }],
    "body": [{
        "description": "test",
        "redirectionUrl": "lik"
      }]
  }]
}]

JSON 2

[
  {
  "Test": [{
    "heading": [{
      "viewmore": "View More",
      "viewmoreURL": "http://google.com"
    }],
    "body": [{
        "description": "test",
        "redirectionUrl": "lik"
      }]
  }]
}]

JSON 3 - have nested json formats

[
  {
  "Test1": [{
    "heading": [{
      "viewmore": "View More",
      "viewmoreURL": "http://google.com"
    }],
    "body": [{
        "description": "test",
        "redirectionUrl": "lik"
      }]
  },
  {
  "Test2": [{
    "heading": [{
      "viewmore": "View More",
      "viewmoreURL": "http://google.com"
    }],
    "body": [{
        "description": "test",
        "redirectionUrl": "lik"
      }]
  }]
    {
  "Test3": [{
    "heading": [{
      "viewmore": "View More",
      "viewmoreURL": "http://google.com"
    }],
    "body": [{
        "description": "test",
        "redirectionUrl": "lik"
      }]
  }]

}]

Above mentioned formats, needs to pass. So I would like to pass the value as below:

@Input() test1:
@Input() test2

Suggest if this is possible.

danday74
  • 52,471
  • 49
  • 232
  • 283
jinfy
  • 147
  • 1
  • 12

2 Answers2

1

Can't see any problems but just in case try this:

<ng-container *ngFor="let j of analytics">
  <app-widget [joke]="j"></app-widget>
</ng-container>
danday74
  • 52,471
  • 49
  • 232
  • 283
  • No still getting same "widget.component.html:4 ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. at NgForOf.push../node_modules/@angular/common/fesm5/common.js.NgForOf.ngDoCheck (common.js:3152)" – jinfy Oct 11 '18 at 13:49
  • OK it sounds like analytics is not an array as expected but is actually something else. If it is an object then you can iterate over object properties with *ngFor using the keyvalue pipe (requires Angular 6.1) - Alternatively, convert it to an array – danday74 Oct 11 '18 at 13:51
  • is there any examples – jinfy Oct 11 '18 at 13:52
  • or just Google convert object to array javascript - returns answers like this ... https://stackoverflow.com/questions/38824349/convert-object-to-array-in-javascript/44790922 - you might want to use lodash to make such an operation easier – danday74 Oct 11 '18 at 13:55
  • is there possible to 2 more @Input decorator – jinfy Oct 11 '18 at 14:08
  • okie, but i tried the same but not able to pass simultaneously – jinfy Oct 11 '18 at 17:32
0

You are passing only one instance of Analytics(which is a single object of analytics) at a time to Child component, So, it is an object which get passed and not the array.

console log of input parameter

Solution: remove the for loop from parent content:

Sumit Vekariya
  • 482
  • 2
  • 12