I am using AngularJS 1.6.4 with Angular-route. I want to set some global variables, like SERVER_PATH which represents the path to the web service of my backend.
I can do this by setting a $rootScope variable in my controller for the main route, but I want it to be loaded if any route is accessed directly.
Here is an example of what I mean:
var myApp = angular.module('myApp', ["ngRoute"])
.config(function ($routeProvider, $locationProvider){
$locationProvider.html5Mode(false);
$routeProvider.
when("/service-provider", {
templateUrl: 'service-provider.html'
}).
when("/confirm", {
templateUrl: 'confirm.html',
controller: 'confirmController'
}).
when("/login", {
templateUrl: 'login/login.html'
controller: 'loginController'
}).
when("/sign", {
resolve: {
"check": function($location, $rootScope) {
if ( !$rootScope.token ) {
$location.path('/login');
}
}
},
templateUrl: 'sign/sign-sp.html',
controller: 'signDataApiCtrl'
}).
when("/verify", {
templateUrl: 'verify/verify.html'
}).
otherwise({
redirectTo: '/login'
});
});
So, /login is my main page. I can set up $rootScope variable in my login controller like this
myApp.controller('loginController', function($rootScope) {
$rootScope.SERVER_PATH = "http://localhost:8080/myApp/webresources";
}
But, what if a user access, for example, "/service provider" page directly. Then, I would need to add this variable to that controller also.
What would be the easiest way to achieve this?