I'm trying to figure out how to make an http get request in order to retrieve a json array of data that I can use within an ngFor
loop. The list that needs to be looped through is stored in this.list
. Currently, the json file for testing purposes is located on my local machine. Despite looking through Angular 2 documentation and taking a udemy course, I am still struggling with this task. I have a feeling that the solution is simpler than I think.
This is the component containing the ngFor
directive:
constructor(private router:Router, private rec:RecommendationsService ){}
ngOnInit(){
this.rec.getRecommendations().subscribe(
data=>{
this.x = JSON.stringify(data);
alert(this.x);
this.list = this.x;
}
);
}
The http get request is handled within a service:
import 'rxjs/Rx';
@Injectable()
export class RecommendationsService{
url="//Users/Daniel/Desktop/Reccomendations.json";
constructor(private http:Http){};
getRecommendations(){
return this.http.get(this.url).map(res => res.json);
}
}
Below is the ngfor
code snippet where the data is needed:
<div *ngFor="let item of list, let i = index" data-toggle="modal" data-target="#recModal">
<div class="row rowTopBuffer rowDiv" (click)="toModal(item.name, item.description, item.recLocLat, item.recLocLong, item.picturePath)">
<div class="col-sm-12 col-md-4 titleDiv" >
<img class="recommendationImages img-responsive img-rounded" src={{item.picturePath}}>
</div>
<div class="col-sm-12 col-md-6 descDiv">
<p class="itemTitle">{{item.name}}</p>
<p id="{{desc+i}}">{{item.description}}</p>
</div>
</div>
</div>
Currently, when running this code, an EXCEPTION: [object Object] error is displayed in the console.