Get current company from subdomain on AngularJS
Web apps like slack have the company name as a subdomain. This can be very useful for giving each company a personalized space. With AngularJS is really easy to get the subdomain and save it on a singleton service.
We first need a service that gets us the subdomain.
angular.module('app').factory('SubdomainService', [
'$location', function($location) {
var service = {};
var host = $location.host();
if (host.indexOf('.') >= 0) {
service.company = host.split('.')[0];
}
return service;
}
]);
Lastly we just need a simple service to hold the value.
angular.module('app').factory('CurrentCompanyService', [
'$q', 'CompanyService', 'SubdomainService', function($q, CompanyService, SubdomainService) {
var deferred = $q.defer();
CompanyService.get_by_slug(SubdomainService.company).then(function(company) {
return deferred.resolve(company);
});
return deferred.promise;
}
]);
Written by Nelson Brandão
Related protips
1 Response
This is just what I need. As I am new to AngularJS could you please tell me how to display the current subdomain in a view?
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Subdomain
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#