Angular - Module
Module with controller, filter, and directive.
by ncapito
HTML
<script src="https://code.angularjs.org/1.5.8/angular.min.js"></script>
<script src="https://code.angularjs.org/1.5.8/angular-resource.min.js"></script>
<script src="https://code.angularjs.org/1.5.8/angular-route.min.js"></script>
<body ng-app="app" ng-controller="EC">
</body>
JavaScript
var app = angular.module('app', []);
app.config(['$httpProvider', function($httpProvider) {
// adding authentiaction token with each request to the server
$httpProvider.interceptors.push(function($q, $window) {
return {
'request': function(config) {
config.headers['Authorization'] = 'bearer ' + $window.localStorage.token;
return config;
}
};
});
}]);
function EC($scope, $http, $window) {
var vm = this;
vm.api = function(resource) {
return ('https://api.website.com/v1/' + resource).replace('//', '/');
};
$window.localStorage.token = 'foobar';
$http.put(vm.api('/users/me'), {
loggedIn: true
})
.then(function(response) {
$http.get(vm.api('/users/me')).then(
function(response) {
vm.user = response.data;
});
});
}
EC.$inject = ['$scope', '$http', '$window'];
app.controller('EC', EC);