I just spent the last hour searching and reading a bunch of similar stackoverflow pages and was unable to find a solution. I'll try to be as descriptive as possible, but please let me know if I need to provide any other details.
Basically, I'm trying to grab a .json file using $http GET. Once I get it, I'll store it in $scope.bags and then on my html (view) page, I am trying to access the contents to use such as title, details, images, etc.
Lets start with the controller:
munyApp.controller('productController', ['$scope', '$location', '$http',
function ($scope, $location, $http) {
var $url2parse = $location.path();
var $urlSplit = $url2parse.split("/");
var $bag2show = $urlSplit.pop();
var $bag2showString = String($bag2show);
console.log($bag2showString);
$http({method: 'GET', url: 'handbags/n1-black-details.json'}).success(function(data) {
$scope.bags = data;
console.log($scope.bags);
$scope.message = "it works!";
});
For now, please ignore the random var lines. I'll get to that later, as I'm guessing it's not related to my issue here.
And here's a small block of the HTML where the controller is to be used:
<div id="detail_large_container" ng-controller="productController">
{{bags.id}}
{{bags['id']}}
</div>
For some reason, I cannot get the id value to show up, which is supposed to be "n1-black".
This may seem really simple (it probably is, I'm a beginner with all of this), but the reason this is all perplexing to me is that I was able to do this same thing for another controller and it works out fine. I copy and pasted the working controller to modify as this newer one, and for whatever reason it doesn't seem to work.
Here's an example of another controller that IS working:
munyApp.controller('handbagsController', ['$scope', '$http',
function ($scope, $http) {
$http.get('handbags/bagsfull.json').success(function(data) {
$scope.bags = data;
});
}]);
With this above controller, it grabs the json file fine and I'm able to use its contents in the html page by using {{bags.somekey}}.
Some curious things that I ran into:
- On my new controller, it failed to fetch the .json file when I used the $http.get() syntax. It only started to fetch it successfully when I changed the syntax to use the $http({method: 'GET', url: ''}) style. Why I had to change it is really confusing to me. I have all my controllers stored in the same .js file and my older ones use the $http.get() fine, while my newer one fails.
- I included a $console.log($scope.bags) after setting $scope.bags to the data that was fetched from the .json file. In my chrome console, it shows that it fetched the .json file fine. It correctly returned my .json file with all the data intact. But when I try to use the data in my html, it's just blank.
- For testing, I set a $scope.message = "it works" inside the $http.success function. When I use {{$scope.message}} in the html, it displays the message correctly. This leads me to believe I'm using the $scope correctly (my understanding of $scope is still pretty limited). So what is boggling is that the .json is being correctly fetched (as I can see in my console), and the html can display the $scope.message but can't use the $scope.data from the .json.
- Lastly, all those random var lines in the controller's code was just a way for me to grab the part of the URL after the last "/". It's using just standard JS code, which I think is okay. Maybe it's not?
Any help or insight regarding this would be greatly appreciated. Please let me know if you need any other information that'd be helpful for this!
Here's the .json file contents below (as requested):
[
{
"id": "n1-black",
"title": "n°1 (Black)",
"description": "TOTE IN TEXTURED CALFSKIN\\nBLACK\\n001",
"images": [
"images/handbags/details/n1-black-1.jpg",
"images/handbags/details/n1-black-2.jpg",
"images/handbags/details/n1-black-3.jpg",
"images/handbags/details/n1-black-4.jpg"
]
}
]
bagsis plural, so I just want to make sure. What is returned in the JSON? Is it a list? A hash? Also, are you sure the content-type header is being returned properly? Your angular code might be parsing the response as a string, which won't have anidpropertybags[1].id