I looked at similar questions, but none of them helped me. I am going to receive an object like the following:
[
{
\"id\": 1,
\"name\": \"Safa\",
In your JSOn file, please make below change.
{
"data":
[
{
"id": 1,
"name": "Safa",
"email": "neerupeeru@mail.ee",
"purpose": "thesis",
"programme": "Software Engineering",
"year": 2016,
"language": "Estonian",
"comments": "In need of correcting a dangling participle.",
"status": "RECEIVED"
},
{
"id": 2,
"name": "Safa",
"email": "neerupeeru@mail.ee",
"purpose": "thesis",
"programme": "Software Engineering",
"year": 2016,
"language": "Estonian",
"comments": "In need of correcting a dangling participle.",
"status": "RECEIVED"
},
{
"id": 3,
"name": "Salman",
"email": "neerupeeru@mail.ee",
"purpose": "thesis",
"programme": "Software Engineering",
"year": 2016,
"language": "Estonian",
"comments": "In need of correcting a dangling participle.",
"status": "RECEIVED"
}
]
}
And after that:
this.http.get(url).map(res:Response) => res.json().data);
The data is actually the name of tge collection of json file. Please try the code above, I am sure it will work.
Store that objects into Array and then iterate the Array
export class AppComponent {
public obj: object =null;
public myArr=[];
constructor(){
this.obj = {
jon : {username: 'Jon', genrePref: 'rock'},
lucy : {username: 'Lucy', genrePref: 'pop'},
mike : {username: 'Mike', genrePref: 'rock'},
luke : {username: 'Luke', genrePref: 'house'},
james : {username: 'James', genrePref: 'house'},
dave : {username: 'Dave', genrePref: 'bass'},
sarah : {username: 'Sarah', genrePref: 'country'},
natalie : {username: 'Natalie', genrePref: 'bass'}
}
}
ngOnInit(){
this.populateCode();
}
populateCode(){
for( let i in this.obj) { //Pay attention to the 'in'
console.log(this.obj[i]);
this.myArr.push(this.obj[i]);
}
}
}
<div *ngFor="let item of myArr ">
{{item.username}}
{{item.genrePref}}
</div>
My solution is create a Pipe for return the values array or propierties object
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'valueArray',
})
export class ValueArrayPipe implements PipeTransform {
// El parametro object representa, los valores de las propiedades o indice
transform(objects : any = []) {
return Object.values(objects);
}
}
The template Implement
<button ion-item *ngFor="let element of element_list | valueArray" >
{{ element.any_property }}
</button>
There you don't need to use this.requests=
when you are making get
call(then requests
will have observable subscription). You will get a response in observable success
so setting requests
value in success make sense(which you are already doing).
this._http.getRequest().subscribe(res=>this.requests=res);
I had the same error because I have mapped the HTTP response like this:
this.http.get(url).map(res => res.json);
Note how I accidentally called .json like a variable and not like a method.
Changing it to:
this.http.get(url).map(res => res.json());
did the trick.
Just declare the var as an array in which you holding the data , it worked for me.
listingdata:Array<any> = [];
this.listingdata = data.results.rows;
and loop the listingdata on html page