json - Load remote data from an AngularJS directive -
i come background of jquery, i'm attempting learn angularjs weather web app i'm building. have 4 select boxes @ top of page list of cities. each select box corresponds weather container below displays current weather selected city.
i'm using directive weather containers shown below. proper way structure this?
<weather city="select1"></weather> <weather city="select2"></weather> <weather city="select3"></weather> <weather city="select4"></weather>
when select atl select box #1 shows atl in weather container select1. part works. need go out server , weather data displayed in directive. have api working can pass atl , returns weather data json. what's proper angular way of doing this? couldn't find in directives call services, ui-router has resolve
. thanks.
angular.module('yourapp') .directive('weather', ['$http', function ($http) { template: '<div data-ng-click="getweather()">{{weatherdata}}</div>', scope: { city: '=' }, controller: function(scope) { scope.getweather = function() { $http.get('/your-weather-api?city=' + scope.city) .success(function(data) { scope.weatherdata = data; }); } } }]);
something work, don't think that's best way structure project. can post code or preferably plnkr, might able more that?
here plnkr showing rough approach may bit easier manage you're after: http://plnkr.co/edit/xcjkfopuiaasgwa3c0lv?p=preview
Comments
Post a Comment