states([{"code":"AL","name":"Alabama"},{"code":"AK","name":"Alaska"},{"code":"AZ","name":"Arizona"},{"code":"AR","name":"Arkansas"},{"code":"CA","name":"California"},{"code":"CO","name":"Colorado"},{"code":"CT","name":"Connecticut"},{"code":"DE","name":"Delaware"},{"code":"DC","name":"District Of Columbia"},{"code":"FL","name":"Florida"},{"code":"GA","name":"Georgia"},{"code":"HI","name":"Hawaii"},{"code":"ID","name":"Idaho"},{"code":"IL","name":"Illinois"},{"code":"IN","name":"Indiana"},{"code":"IA","name":"Iowa"},{"code":"KS","name":"Kansas"},{"code":"KY","name":"Kentucky"},{"code":"LA","name":"Louisiana"},{"code":"ME","name":"Maine"},{"code":"MD","name":"Maryland"},{"code":"MA","name":"Massachusetts"},{"code":"MI","name":"Michigan"},{"code":"MN","name":"Minnesota"},{"code":"MS","name":"Mississippi"},{"code":"MO","name":"Missouri"},{"code":"MT","name":"Montana"},{"code":"NE","name":"Nebraska"},{"code":"NV","name":"Nevada"},{"code":"NH","name":"New Hampshire"},{"code":"NJ","name":"New Jersey"},{"code":"NM","name":"New Mexico"},{"code":"NY","name":"New York"},{"code":"NC","name":"North Carolina"},{"code":"ND","name":"North Dakota"},{"code":"OH","name":"Ohio"},{"code":"OK","name":"Oklahoma"},{"code":"OR","name":"Oregon"},{"code":"PA","name":"Pennsylvania"},{"code":"RI","name":"Rhode Island"},{"code":"SC","name":"South Carolina"},{"code":"SD","name":"South Dakota"},{"code":"TN","name":"Tennessee"},{"code":"TX","name":"Texas"},{"code":"UT","name":"Utah"},{"code":"VT","name":"Vermont"},{"code":"VA","name":"Virginia"},{"code":"WA","name":"Washington"},{"code":"WV","name":"West Virginia"},{"code":"WI","name":"Wisconsin"},{"code":"WY","name":"Wyoming"}])
I just can't figure out how to load this in correctly and parse it. If it was in standard JSON - its no problem - I just read it in whith an $http method and save the returned data as an object and go from there.
With JSONP though - it seems just a little more complex. I think I either need to set up a callback or something to execute the states function, but I am just lost at how to do that.
Here is the angular Code that I have reading in a standard JSON file now:
In the main-controller.js
'use strict'; angular.module('locationApp') .controller('MainCtrl', function ($scope, ReadJson)
{
// Variable setup
$scope.states = [];
// Get the States
ReadJson.getJson('http://massachusettswebdesigns.com/states.json').then(function (data) {
$scope.states=data;
console.log("States " + $scope.states);
}); // end of getJson for states
});
angular.module('locationApp').
factory("ReadJson", ['$http', function ReadJsonFactory($http)
{
return {
// GetJson - Gets a JSON based URL and returns it as an object.
getJson:function(url)
{
return $http.get(url).then(function (result)
{
// console.log("Result = " + result.data);
return result.data;
});
}
}
}]);
angular.module('locationApp').
factory("ReadJson", ['$http', function ReadJsonFactory($http)
{
return {
// GetJson - Gets a JSON based URL and returns it as an object.
getJson:function(url)
{
return $http.jsonp(url).then(function (result)
{
console.log("Result = " + result.data);
return result.data;
});
}
}
}]);
angular.module('locationApp')
.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.states = [];
$scope.cities = [];
// Function for the States callback on the JSON files provided
window.states = function (data)
{
$scope.states = data;
};
// function for the cities callback on the JSON files proviced
window.cities = function (data)
{
$scope.cities = data;
};
// Get the State JSON file.
$http.jsonp('http://locationinc-mapping-demo.herokuapp.com/states.json');
}]) // end of MainCtrl