0

I am caught up in a situation here.

I have a switch in my login page and i want to redirect my service for authorization accordingly.

I want to load my services dynamically after checking some conditions, when i launch my application for the first time. But in angular as we have ng-app and we need to inject all the modules in it at the start. My base url is set before the app is launched. Is it possible to change my base url on condition?

On launching the application the base url gets assigned before the launch of first page which is login page. I have a switch in my login page which if true, i need to set the different base url. But since the base url is set and the control doesnt come again to this module, i am not able to change it conditionally.

This is in my service.js

angular.module('sampleService', ['ngResource'])
       .factory('sample', function ($resource, $rootScope) {
           $rootScope.serviceUrl = "http://......";
               ...
       });

This is in my app.js

var app = angular.module("sampleApp", ["sampleService"]);

This is how I am using $routeProvider

app.config(["$routeProvider", function ($routeProvider, $locationProvider) {
    ...
}]);

I want to change my service url conditionally as:

if(myflag)
    $rootScope.serviceUrl = "url1";
else
    $rootScope.serviceUrl = "url2";

I hope this will give some idea of what i want to do and what i am doing.

Thanks in advance.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
tanuj
  • 91
  • 1
  • 7

2 Answers2

1

I suppose you're using ngRoute in your application, right?

Maybe you should look into uiRouter where you control state. And what you're describing could fall into this category.

https://github.com/angular-ui/ui-router

Also read the differences between the two in this Stackoverflow content.

Community
  • 1
  • 1
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • Thanks for the links Robert. Is there any other way to achieve this functionality. Since the application is fully developed using ngRouter and its a huge application. So it will be total restructuring which we dont want to do as its going for production in few days. – tanuj Sep 03 '14 at 09:36
  • @tanuj provide additional information/details how exactly things should work. Provide URLs and specifics, so we can suggest something useful. – Robert Koritnik Sep 03 '14 at 11:18
  • i have edited the problem, please let me know if you want more info. Thanks. – tanuj Sep 03 '14 at 11:47
  • So by the state of things you're using your service's URL as stored in your `$rootScope`? If you do I don't know what seems to be the problem then. Use that. – Robert Koritnik Sep 10 '14 at 12:54
1

Yes uiRouter is best solution when it comes to dynamic page loading

Here is a simple PDF on how to use ui-sref in your code , Simple Example

  • Thanks Jan but it will still need restructuring since we have already used ngRouter. And we are out of time here. Can we get this without replacing ngRouter with ui-router? – tanuj Sep 03 '14 at 10:14