I have an Angular app with a controller that accesses a service. I have the service properly sending data back to my controller, but in the .then
function I can't seem to access specific elements of the json data.
Controller (snippet)
vm.getData = function(eventid) {
monitorData.requestEvent(eventid)
.then(function(data) {
// console.log(data);
vm.event.id = data.id;
})
.catch(function(e) {
console.log("Error");
});
return false;
};
vm.getData($routeParams.eventid);
Sample Data
{
id: "123456",
type: "pokemon",
time: "July 13, 2016 at 04:00 PM Eastern Daylight Time",
oneURL: "www.junk1.com",
twoURL: "www.junk2.com",
threeURL: "www.junk3.com",
phone: "1 (555) 765-4321",
notes: "some junk here",
}
if I do console.log(data);
it prints into my browser console. But I tried setting a variable equal to data
then printing it outside the function, and it won't. Nor can I access any specific parts of the json object.
I'm sure there is a simple explanation for this, but I can't seem to find it anywhere.
...Edit (adding the service code)
(function() {
angular
.module('monitorApp')
.service('monitorData', monitorData);
monitorData.$inject = ['$http'];
function monitorData($http) {
var requestEvent = function(eventid) {
return $http.get('/api/event/' + eventid);
};
return {
requestEvent : requestEvent,
};
}
})();
...Edit for clarification
Because of previous confusion by a user, let me try to be more clear: In my controller code above you'll see
.then(function(data) {
// console.log(data);
vm.event.id = data.id;
})
Inside of this snippet, you'll see data
. That data contains the object I have listed above (Sample Data).
My question is, is there some way to further access id, type, time, ... phone, notes
. I can console.log(data)
and it will print the object out in the console (for testing purposes), but I need to access each item in the object and use it outside the entire vm.getData
function.
Someone previously said "it's not possible", would others agree?