_4ck1q
Last Updated: February 25, 2016
·
2.124K
· nelsonbrandao

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;
  }
]);

1 Response
Add your response

21938

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 ·