8

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.

2 Answers 2

21

For Settings related information, I use constant:

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

Here is a simple plunker to illustrate constant.

Sign up to request clarification or add additional context in comments.

1 Comment

I discovered constant few minutes after I asked my question. But your example is way clearer than Angular doc :) Thanks!
2

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

3 Comments

Thanks for the tip. I forgot about this. I will pick for @marcoseu answer since it's great in a more generic way.
having a general constant to have a "base path" is just insane
Obviously, and hopefully, I'm not just doing that. I do have more constants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.