There are several ways to use constants in angular:
Using angular.constant
angular.module('myApp.config', []). constant('APP_NAME', 'MyApp'); angular.module('myApp.controllers', ['myApp.config']) .controller('AppCtrl', ['$scope', 'APP_NAME', function($scope, appName) { $scope.val = appName; }]);
Using angular.value
angular.module('myApp.config', []). value('config', { appName: 'AppName' }); angular.module('myApp.controllers', ['myApp.config']) .controller('AppCtrl', ['$scope', 'config', function($scope, config) { $scope.val = config.appName; }]);
Or the way you did this, but the factory is very often used to configure something once before returning the configuration object, for example for $ add some dependencies (for example, $ locale).
In addition, I often use the consts directive to attach constants in the area in which I want:
angular.module('myApp.config', []). directive('consts', function(config) { return { restrict: 'A', link: function($scope) { $scope.config = config; } } });
And then:
<div consts> <h2>{{config.appName}}</h2> </div>
source share