In my controller I have the following...
$scope.products = dataService.getJsonData();
console.log($scope.products);
and in my dataservice I have the following
.service('dataService', function ($http) {
this.getJsonData = function () {
return $http.get('path/to/json/products.json').then(function (response) {
// don't send back everything....
var productData = response.data;
console.log(productData);
return productData;
});
};
and in my view I have the following...
<div ng-repeat="product in products">
Name: {{ product.name }} <br>
Price: {{ product.price }}
<hr>
</div>
in my rendered view the repeat is showing only 3 items (products is an array of 15 objects). When looking at the console the repeat in the view or products is made of Object { then=function(), catch=function(), finally=function()} but the log from the dataservice is out putting the desired object array. I don't understand why the output isn't waiting for the returned data, I thought this was asynchronous? How can I make the view wait for the dataservice without using a $timeout. Has anyone else had this problem? Thanks in advance.
UPDATE *
From a bit of googling I'm pretty sure I need to add a resolve to my $routeProvider, the $routeProvider currently looks like so:
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl',
resolve:{
// i need to put something here...
}
})