-1

I want to generalize my state code below to pass in a string to serve as an argument into load and that argument will be called module which is then parsed in load call for lazy loading. Simply adding a string will error because Angular thinks it is a provider and triggers an unknown provider exception.

How can I achieve this objective?

function load ($ocLazyLoad, $q, $stateParams, module){
    var deferred = $q.defer();
    try{
        $ocLazyLoad.load(module).then(function(){
            deferred.resolve();
        });
    }
    catch (ex){
        deferred.reject(ex);
    }
    return deferred.promise;
}


 $stateProvider
    .state('action', {
        name: 'action',
        url: "/actionitems",
        resolve: {
               loadDependencies: ['$ocLazyLoad', '$q', '$stateParams', load]
        },
        templateUrl: '/app/tool/action/ActionItems.html'
  });
Vahe
  • 1,699
  • 3
  • 25
  • 76

1 Answers1

1

You need to create a constant provider. You can do something like this:

angular.module('my-module').constant('myConstant', 'my-value');

And then in your state provider:

$stateProvider
    .state('action', {
        name: 'action',
        url: "/actionitems",
        resolve: {
               loadDependencies: ['$ocLazyLoad', '$q', '$stateParams', 'myConstant', load]
        },
        templateUrl: '/app/tool/action/ActionItems.html'
  });

function load ($ocLazyLoad, $q, $stateParams, myConstant, module){
  // myConstant has the value 'my-value'
  ...
}
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148
  • Thank You, seems my approach is complicating the issue. I have referenced the question from a different angle here: https://stackoverflow.com/questions/55564581/angularjs-how-do-i-get-the-state-name-inside-of-state-resolve-function-to-load/55564770#55564770 to attempt to keep my code DRY but I still have left to find an answer – Vahe Apr 08 '19 at 05:46