Reputation: 2823
I have a post function that gets my json.
I want to print out all the
max_slots
, base_image
and c_name
as they are in a array I am trying to loop though save value to scope to use in my HTML.
Am I doing this right ? if not whats the best way to do this ?
OBJECTIVE
display the results of the json array post in my html
What I am trying
$scope.c_name = [0];
$scope.GetData = function () {
$http({
url: "http://www.ccuktech.co.uk/ccuploader/campaigns/getCampaign",
method: "POST",
date: {},
headers: {'Content-Type': 'application/json'}
}).then(function (data) {
// success
console.log('you have received the data ');
console.log(data);
// $scope.base_image = response.base_image; $scope.c_name = response.c_name;
$scope.c_name = data.data.c_name;
// for(var i = 0; i < c_name.length; i++) {
// var obj = c_name[i];
//
// console.log(obj.id);
// }
// $scope.max_slots = data.data.max_slots;
// $scope.slot_image = data.slots.base_image;
// console.log($scope.c_name);
}, function (response) {
// failed
console.log('failed getting campaigns goo back to log in page.');
console.log(response);
});
};
My HTML
<body ng-app='myApp'>
<h2>name</h2>:
{{c_name}}
<h2>max slots</h2>:
{{max_slots}}
<h2>image</h2>:
{{base_image}}
</body>
JavaScript
$scope.GetData = function () {
$http({
url: "http://www.####.co.uk/####/####/####",
method: "POST",
date: {},
headers: {'Content-Type': 'application/json'}
}).then(function (data) {
// success
console.log('you have received the data ');
console.log(data);
// $scope.base_image = response.base_image; $scope.c_name = response.c_name;
// $scope.c_name = data.data.c_name;
for(var i = 0; i < c_name.length; i++) {
var obj = c_name[i];
console.log(obj.id);
}
// $scope.max_slots = data.data.max_slots;
// $scope.slot_image = data.slots.base_image;
// console.log($scope.c_name);
}, function (response) {
// failed
console.log('failed getting campaigns goo back to log in page.');
console.log(response);
});
};
$scope.GetData();
console.log(data) result
Upvotes: 0
Views: 1162
Reputation: 5217
Since the data
in your API response is an Array. You can not just retrieve the values with dots
Try changing $scope.c_name = data.data.c_name;
with $scope.c_name = data.data[0].c_name;
and so on.
OR
$scope.responseData = data.data[0];
And in your html
<body ng-app='myApp'>
<h2>name</h2>:
{{responseData.c_name}}
<h2>max slots</h2>:
{{responseData.max_slots}}
<h2>image</h2>:
{{responseData.base_image}}
</body>
EDIT: Considering chance for occurring more than one element in data
array
$scope.responseData = data.data;
and in the HTML:
<body ng-app='myApp'>
<div ng-repeat="data in responseData">
<h2>name</h2>:
{{data.c_name}}
<h2>max slots</h2>:
{{data.max_slots}}
<h2>image</h2>:
{{data.base_image}}
</div>
</body>
Upvotes: 1