Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Settings in AngularJS

Tags:

angularjs

I have an app that have to be customisable and one parameter is the root of the url. The app ain't necessarily at the root of the website, ie. it can be hosted at http://onedomain.com/index.html, where the appName would be /, as it can be hosted at http://anotherdomain.com/myapp/index.html, where the appName would be /myapp/.

But I need to know the appName in the router, so in the configFn of my module, to do this kind of stuff:

return $routeProvider.when(appName + "index.html", {
  templateUrl: 'views/main.html',
  controller: 'MainCtrl'
})

As I have more parameters, I started a service I called Settings but you can't inject services while configuring a module…

What would you do?

For my concern, I started thinking about a custom provider but I'm not sure it's appropriate.

like image 976
Adrien Avatar asked Jun 12 '13 18:06

Adrien


People also ask

What is$ http in AngularJS?

$http is an AngularJS service for reading data from remote servers.

What is factory in AngularJS?

What is Factory in AngularJS? Factory is an angular function which is used to return the values. A value on demand is created by the factory, whenever a service or controller needs it. Once the value is created, it is reused for all services and controllers. We can use the factory to create a service.

What is providers in AngularJS?

A provider is an object with a $get() method. The injector calls the $get method to create a new instance of a service. The Provider can have additional methods which would allow for configuration of the provider. AngularJS uses $provide to register new providers.

What is rootScope in AngularJS?

All applications have a $rootScope which is the scope created on the HTML element that contains the ng-app directive. The rootScope is available in the entire application. If a variable has the same name in both the current scope and in the rootScope, the application uses the one in the current scope.


2 Answers

For Settings related information, I use constant:

angular.module(...)
  .constant("APPNAME", "/myapp/")
  .controller(..., function(..., APPNAME) {...})

Here is a simple plunker to illustrate constant.

like image 74
marcoseu Avatar answered Oct 02 '22 01:10

marcoseu


Just use .when('/' and <base href="/myapp/" />.

like image 40
Ven Avatar answered Oct 02 '22 01:10

Ven