0

So in angular it is possible to do something like this:

function userTemplateProvider($sessionStorage, $templateRequest) {
 var user = $sessionStorage.user;
}

When you call the function you simply type: userTemplateProvider; and angular will automaticly inject the services.

Now im in a situation where i need to pass variables to this function.

if i do:

userTemplateProvider($sessionStorage, $templateRequest, myvar);

And ofcourse add the myvar to the function:

    function userTemplateProvider($sessionStorage, $templateRequest, myvar){
 var user = $sessionStorage.user;
}

Then the two services will be empty.

So my question is how do i add variables but still inject the services

My full code:

Dashboard Module

angular.module('Dashboard',[])

.config(['$stateProvider', '$urlRouterProvider', 'JQ_CONFIG', 'USER_ROLES', 'hammerDefaultOptsProvider',
    function ($stateProvider, $urlRouterProvider, JQ_CONFIG, USER_ROLES, hammerDefaultOptsProvider) {
            $stateProvider
            .state('dashboard', {
                abstract: true,
                url: '/dashboard',
                templateProvider: userTemplateProvider,
                resolve: {
                    deps: ['uiLoad',
                        function (uiLoad) {
                            return uiLoad.load([
                                'js/controllers/headerController.js'
                            ]);
                        }]
                }
            })
            .state('dashboard.index', {
                url: '/index',
                templateProvider:getTemplate,
                data: {
                    authorizedRoles: [USER_ROLES.lb, USER_ROLES.superadmin, USER_ROLES.subadmin]
                },
                resolve: {
                    deps: ['uiLoad',
                        function (uiLoad) {
                            return uiLoad.load([
                                'js/controllers/chart.js',
                                'js/controllers/dashboard/DashboardController.js',
                                'js/controllers/dashboard/ClientDashboardController.js'
                            ]);
                        }]
                }
            })

    }]);

TemplateLoader

    angular.module('TemplateLoader', []);

function userTemplateProvider($sessionStorage, $templateRequest) {
    var templateLocation = $sessionStorage.activeUser.user.user_type.super_type_id == 1 ? 'tpl/app.html' : 'tpl/client/client.html';
    return $templateRequest(templateLocation);
}
function getTemplate($state, $sessionStorage) {
    var templateLocation = null;
    switch ($sessionStorage.activeUser.user.user_tpe.super_type_id) {
        case 1:
            break;

        case 2:
            break;

        default:
            break;
    }
    return $templateRequest(templateLocation);

}
4
  • 1
    I can't reproduce/comprehend what you're asking. In what context is this function used? Who is calling it? Is this a controller? A service/provider? A regular function? Commented Apr 5, 2016 at 8:56
  • @deceze il add my full code: Commented Apr 5, 2016 at 8:56
  • use a service or a factory. Commented Apr 5, 2016 at 8:58
  • @NickD i can't i use this in the routing which is loaded before services and factories Commented Apr 5, 2016 at 8:59

1 Answer 1

2

Creating a function specialised to myvar could work, though there is probably a convention for Angular that you could use instead.

function userTemplateProvider(myvar) {
  return function($sessionStorage, $templateRequest) {
    var user = $sessionStorage.user;
    // can also access myvar here
  };
}

Then us it as:

userTemplateProvider(myvar);
Sign up to request clarification or add additional context in comments.

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.