I'm facing a challenge with displaying JSON items that have an array of different ids at the end of the URL (/api/messages/:messageId). For instance, accessing /api/messages/12345 would return {"subject":"subject12345","body":"body12345","id":"12345"}.
I initially thought about looping through the data and appending it to 'api/message/' to achieve this. However, I'm struggling to define the data before making the $http.get call.
What is the most effective way to define the data for this to work? Are there any alternative solutions you can suggest?
Here's the AngularJS code snippet:
var countryApp = angular.module('countryApp',[]);
countryApp.controller('CountryCtrl', function($scope, $http){
for(i=0;i<data.length;i++){
var messageId = data[i].id;
console.log(messageId);
// data is undefined here
}
$http.get('/api/messages/' + messageId ).success(function(data){
$scope.messages = data;
$scope.expand = function (message) {
angular.forEach($scope.messages, function (currentItem){
currentItem.showfull = currentItem == message && !currentItem.showfull;
});
};
});
});
JSON examples:
// from api/messages/12345:
{"subject":"subject12345","body":"body12345","id":"12345"}
// from api/messages/123456789
{"subject":"subject123456789","body":"body123456789","id":"123456789"}
// And so forth...