AngularJS/Dependency 注入 - 如何在依赖项提供程序数组中传入一个简单的字符串文字以作为值使用?

AngularJS/Dependency Injection - How do I pass in a simple string literal in the dependency provider array for consumption as a value?

我想概括我下面的状态代码以传入一个字符串作为加载的参数,该参数将被称为模块,然后在加载调用中解析该模块以进行延迟加载。简单地添加一个字符串会出错,因为 Angular 认为它是一个提供者并触发一个未知的提供者异常。

我怎样才能做到这一点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'
  });

您需要创建一个 constant provider。你可以这样做:

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

然后在您的州提供者中:

$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'
  ...
}