angularjsangularjs-routingangularjs-providerangularjs-rootscope

Adding templateUrl dynamically while setting $routeProvider $ angular.config


I am angular newbie.I would like to achieve following code...

 $routeProvider.when('/view', {templateUrl: 'ViewSwitcher?pageId='+$rootSope.pageId+'&userId='+$rootSope..userId+'&token='+$rootScope.token, controller: ''});

ViewSwitcher is a servelet which responses me a HTML page as per pageId,userId(saved in $rootScope) ......but $rootScope is not available....Thanks in advance!


Solution

  • You could do one thing here instead of store this variables inside the $rootScope use provider that could be easily accessible inside the config phase. Create one myData provider that would share a data between different components of your app.

    Code

    //before using `myDataProvider` make sure it has been injected as dependency.
    $routeProvider.when('/view', {
        templateUrl: 'ViewSwitcher?pageId='+myDataProvider.pageId+'&userId='+ myDataProvider.userId+'&token='+ myDataProvider.token,
        controller: 'myCtrl' //<--here it should be some controller
    });