I'm creating a dashboard and I wanted to add this USA map and when I click on any of the region I want it's population to be printed out on the web page from the cities.json file.
Service for importing the json file.
App.factory('citiesData', ['$http', function($http) {
return {
getMessageData : function () {
return $http({ method: 'GET', url: '../app/_data/usa-cities.json'})
.then(function(response) {
return response.data;
}, function(errorResponse) {
throw errorResponse.status;
});
}
}
}]);
I created a directive for the jvectormap plugin and everything's working just fine.
Here's my directive code.
App.directive('mapUsa', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.vectorMap({
map: 'us_aea_en',
backgroundColor: 'transparent',
zoomButtons: false,
regionStyle: {
initial: {
fill: '#343434'
},
hover: {
fill: '#242424'
}
},
onRegionClick: function(event, code) {
},
markerStyle: {
initial: {
fill: '#F0A518',
stroke: '#F0A518',
}
},
markers: [
{latLng: [37.78, -122.41], name: 'San Francisco', style: {r: 10}},
{latLng: [40.71, -74], name: 'New York City', style: {r: 15}},
{latLng: [41.89, -87.62], name: 'Chicago', style: {r: 5}},
{latLng: [34.00, -118.25], name: 'Los Angeles', style: {r: 20}},
{latLng: [34.00, -106.00], name: 'New Mexico', style: {r: 10}},
{latLng: [44.50, -100.00], name: 'South Dakota', style: {r: 13}},
{latLng: [25.78, -80.22], name: 'Miami', style: {r: 7}},
]
});
}
}
}])
Sample JSON :
{ "US-AL": { "name": "Alabama", "population": 4833722 }, "US-AK": { "name": "Alaska", "population": 735132 }
HTML :
<div class="col-md-8">
<div class="map-container padding-all-15">
<div class="map-usa margin-bottom-15" map-usa></div>
</div>
</div>
<div class="col-md-4">
<div class="header-container">
<h4>Population</h4>
<span></span>
</div>
</div>
The code param in the onRegionClick returns me the code and I created a object for each code in the cities.json file.
- I wanted to know how can I not access that service in a controller inside the directive.
- How can I access the
onRegionClickto get the code and retrieve the respective data of the region clicked.
- Using a jquery plugin and trying to use the data binding of angular is the correct method or is there any other method that has to be followed for this scenario?
Thanks in advance.