0

This is my method to call a local JSON file. However, the JSON can be seen in the browser console but nothing is being seen in the HTML page, can anyone see anything I am missing in my code. Thanks

  loadUser(){

  this.http.get('assets/json/collection.json')
  .map(res => res.json())
  .subscribe(data => {
    this.data = data.results;
    console.log(data.results);
  });
}


    <div *ngFor="let result of results">   
        <p>{{results.title}}</p>
    </div>

2 Answers 2

1

Guessing that your loadUser() function is part of your component class, you probable have to call "data" in your template, as you assign data.results to this.data

<div *ngFor="let result of data">   
    <p>{{result.title}}</p>
</div>
Sign up to request clarification or add additional context in comments.

8 Comments

cannot find a differ supporting object [object Object] is the error i get when i change it to <div *ngFor="let result of data"> what ive read is that its how the json is coming back as an object
Can you paste the console.log result please?
loadUser(){ this.http.get('assets/json/collection.json') .map(res => res.json()) .subscribe(data => { this.data = data.results; console.log(data.results); }); } ionViewDidLoad() { console.log('ionViewDidLoad HomePage'); this.loadUser(); }
@scipper 's answer is right, I think. When is ionViewDidLoad() called? I think you should call this.loadUser() in ngOnInit()
I'm getting the error "Cannot find a differ supporting object [ object Object]" of type 'object'. NgFor only supports binding o iterables such as arrays. When I change to match scipper's suggestion?
|
0

I think you need to make it so this.data is an Array of type result. So you would have to set up a model called Result. Also if your JSON has more than one result, you should make something like this:

collection.json

{
  "results":[ 
       {
         "id": 20,
         "title": "asdasd",
         "identifier": null,
         "colour_bg": "#bd2c2c",
         "colour_btn": "#a11212",
         "colour_content_bg": "#ffffff", 
         "logo_dashboard": "collection/21/SVwnz6tjluX81dIRNXjZo7issYUWyOVyD3LJFykm.png"‌​,
         "logo_page": "collection/21/txgkqlkPA45mHSYtqZ5rFa4wLfHNUfXESE0xyhwp.png"‌​
       },
       {
         "id": 21,
         "title": "Arsenal Test Collection",
         "identifier": null,
         "colour_bg": "#bd2c2c",
         "colour_btn": "#a11212",
         "colour_content_bg": "#ffffff", 
         "logo_dashboard": "collection/21/SVwnz6tjluX81dIRNXjZo7issYUWyOVyD3LJFykm.png"‌​,
         "logo_page": "collection/21/txgkqlkPA45mHSYtqZ5rFa4wLfHNUfXESE0xyhwp.png"‌​
       }
    ]
 }

result.ts

import { Injectable } from '@angular/core';

@Injectable()
export class Result{

   id: number; 
   title: String;
   identifier: String; 
   colour_bg: String;
   colour_btn: String; 
   colour_content_bg: String; 
   logo_dashboard: String;
   logo_page: String;

   constructor(){
   }

}

Then in your Page

page.ts

import { Result } from 'path/to/your/model/folder/result/
data: Array<Result> = [];

loadUser(){
   this.http.get('assets/json/collection.json')
   .map(res => res.json())
   .subscribe(data => {
      this.data = data.results;
      console.log(data.results);
   });
}

EDIT

You could also set up each Result object inside the subscribe and then push them into a final array.
IMPORTANT: Call loadUsers inside ionViewWillEnter(), so it runs every time you enter the page.

loadUsers()  {
    let results = [];
    var i;
    this.http.get('assets/json/collection.json')
    .map(res => res.json())
    .subscribe(data => {
      for(i=0; i< data.results.length;i++) {
          let result = new Result();
          // add each attr to the result object
          // i.e.
          result.id = data.results[i].id
          //push the object to the final array
          results.push(result)
        }
      })
      this.data = results;
    }

8 Comments

Will the HTMl output like so <div *ngFor="let item of results"> <p>{{item.id}}</p> </div>
The JSON is still showing in the browser but not in the HTML page?
Did you add the array to the json?
The other thing you can do is manually set each object inside the subscribe and push them to an array
Sorry how do you mean?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.