Can $q and $http be injected in the .config section

前端 未结 3 622
余生分开走
余生分开走 2020-12-18 20:58

Is it possible to inject $q in the config section of my module? Below is my sample config section.

.config([\'$q\', function ($q) {
    var func = function (         


        
相关标签:
3条回答
  • 2020-12-18 21:21

    You can use angular.injectorto load $http and $q, and probably other services in your config block:

    angular.module('myApp').config(function () {
        var injector = angular.injector(['ng']),
            http = injector.get('$http'),
            q = injector.get('$q');
    });
    
    0 讨论(0)
  • 2020-12-18 21:22

    It is possible for me (when routing configure):

    resolve: {
       simpleStringParam: ["$q", "$timeout", function($q, $timeout){
          var deferred = $q.defer();
              $timeout(function(){
                  deferred.resolve("Allo!");
              },8000);
          return deferred.promise;
       }]
    }
    
    0 讨论(0)
  • 2020-12-18 21:35

    Correct--you can't inject $http or $q from a config function. They are not available yet (they're also being configured!).

    0 讨论(0)
提交回复
热议问题