Angular test
by Paweł Niemczyk
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular-resource.js"></script>
<body ng-app="app">
<div ng-controller="TestController as testCtrl">
<input type="text" ng-model="testCtrl.text">
<button ng-click="testCtrl.checkWeather(testCtrl.text)">check weather</button>
<button ng-click="testCtrl.$log.log(testCtrl.text)">log</button>
<button ng-click="testCtrl.$log.error(testCtrl.text)">error</button>
<button ng-click="testCtrl.$log.warn(testCtrl.text)">warning</button>
<button ng-click="testCtrl.$log.info(testCtrl.text)">info</button>
<button ng-click="testCtrl.$log.debug(testCtrl.text)">debug</button>
<br />
<label>{{testCtrl.text}}</label>
</div>
</body>
JavaScript
(function(){
angular
.module("app", ['ngResource'])
.service('WeatherService', ['$resource', WeatherService])
.controller("TestController",['$scope', '$log', 'WeatherService', TestController]);
function TestController($scope, $log, WeatherService) {
var vm = this;
vm.$log = $log;
vm.checkWeather = checkWeather;
function checkWeather(city){
WeatherService({params: { q: city}}).get().$promise.then(function(data){
$log.info(data);
});
};
};
function WeatherService($resource){
var url = 'http://api.openweathermap.org/data/2.5/weather';
var defaultParams = { q: 'London', appid: '2d92a31d3186df6cca3fe822f26015db'};
return function(weatherConfig){
var params = angular.extend({}, defaultParams, weatherConfig.params || {});
var weatherResource = $resource(url, params, {get: {method: 'GET', isArray: false}});
return weatherResource;
};
}
})();