0

I'm new to angular and I'm working with an Angular app that has some initial settings, just lot of variables containing thinks like Api authorizations, names, routes and more. Right now all those are just var declared after this part:

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

There is a recommended place where all those variables must go?

1

2 Answers 2

1

Module

In angular you can have a sort of main module that you add your configuration too.

This module will be injected with all of the other modules you create as well as modules like NgRoute. Here is an example of a main module in my angular app, full source here

var app = angular.module("myApp",   [
 'ui.bootstrap',
 'ngAnimate',
 'myAppRouter',
 'myAppHomeCtrl',
 'myAppHomeService',
 'myAppNavbarDirective',
 'myAppNavbarService',
 'myAppLoginCtrl',
 'myAppLoginService'
 ]);

//This config is used to remove the # in the html
app.config(["$locationProvider", function($locationProvider) {
   $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
   });
}]);

The above shows config on my Main module to get my URLs to look correct.

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

2 Comments

I'm going with this one because was the best for my case. Thanks!
Sergio just message me on here if you have any questions. I'm glad to be of help :)
1

You define these things using angular constants and values.

https://docs.angularjs.org/api/auto/service/$provide#value

Once you've create a constant or value you can inject them into your controllers and directives.

Sometimes you need data passed into your app via the rendered index.htlm. For example; a .NET application might want to pass some data from web.config to the angularjs app.

You can append a block after the include for your app and declare the data there.

 <script>angular.module('app').constant('websiteName','HELLOWORLD');</script>

Comments

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.