0

I'm creating an App that different customers should use. The things that will differ between the customers is for example the brand name and the server URL.

I have seen the constant() method, but I don't really understand it.

For example:

<body>
    Welcome to {{brandName}} homepage. 
    We use the server {{server.url}} for our REST calls.
</body>

I'm thinking a file conf.js

var brandName = "Örkelljunga IF"
var server = {
    "url": "Orkelljunga.com",
    "otherinfo" : "Other Information"
}
export.brandName = brandName;
export.server = server;

And then access it from constant()? How do I achieve this?

Any examples and maybe best practices in Angular?

petur
  • 1,366
  • 3
  • 21
  • 42

3 Answers3

1
angular
  .constant("myConfig", export)
  .controller("mainCtrl", function ($scope, myConfig) {
    $scope.brandName = myConfig.brandName;
    $scope.server = myConfig.server;
  });
Vanni Totaro
  • 5,211
  • 2
  • 28
  • 42
1

You can make a controller and bind this data to $scope. Something like this:

var myApp = angular.module('myApp',[]);

myApp.controller('MyController', ['$scope', function($scope) {
  $scope.brandName = "Örkelljunga IF"
  $scope.server = {
    "url": "Orkelljunga.com",
    "otherinfo" : "Other Information"
  }
}]);

You'll have to include following in your html too.

<html lang="en" data-ng-app="myApp">

and

<div ng-controller="MyController">
  Welcome to {{brandName}} homepage. 
  We use the server {{server.url}} for our REST calls.
</div>
khizar naeem
  • 296
  • 2
  • 6
1

You can add a simple config object that you inject wherever it is needed. Take a look at this question/answer:

Community
  • 1
  • 1
Lee Willis
  • 1,552
  • 9
  • 14