1
\$\begingroup\$

I am developing an application that has different configurable versions - "a1" and "a2". The version name is initially being set from the database as "a1" in the $rootScope.

I reference the version name in every templateProvider in UI-Router, which ends up being over 30 times:

.state('app.auto.public.inventory.list', {
    url: '/',
    views: {
        '[email protected]': {
            templateProvider: ['$rootScope', '$templateCache', function($rootScope, $templateCache) {
            return $templateCache.get($rootScope.autoVersion + '/html/inventory/list/index.html');
        }],
        controller: 'InventoryList'
        }
    }
})

There must be a better way to access the value of "a1", instead of referencing $rootScope.autoVersion so many times?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

A better solution is to resolve the data in a parent route, then inject the resolved data ("identityResolve") from a service ("identity") into each child instance.

.state('app.auto', {
    abstract: true,
    url: '',
    resolve: {
        identityResolve: ['identity', function(identity) {
            return identity.initialize().$promise;
        }]
    }
})

Which then is used like this

.state('app.auto.public.inventory.list', {
    url: '/',
    views: {
        '[email protected]': {
            templateProvider: ['$templateCache', 'identityResolve', function($templateCache, identityResolve) {
                return $templateCache.get(identityResolve.version + '/html/inventory/list/index.html');
        }],
        controller: 'InventoryList'
    }
});
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.