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.
$http is an AngularJS service for reading data from remote servers.
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.
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.
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.
For Settings
related information, I use constant:
angular.module(...)
.constant("APPNAME", "/myapp/")
.controller(..., function(..., APPNAME) {...})
Here is a simple plunker to illustrate constant.
Just use .when('/'
and <base href="/myapp/" />
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With