【问题标题】:Unable to set the http defaults inside the angularjs factory无法在 angularjs 工厂中设置 http 默认值
【发布时间】:2016-06-03 09:51:54
【问题描述】:

在我的 Angularjs 应用程序中,我有一个工厂,我需要对用户进行身份验证。我正在使用httpProvider 设置默认值。但我收到错误消息,指出 $httpProvider 未定义。

'use strict';
 angular.module('myApp')
.factory('authService', function ($q, $http, $rootScope, $window) {
    return {
        authenticateUser: function () {
           ........
           ........
       $httpProvider.defaults.headers.common['Accept'] = 'application/json, text/javascript';
       $httpProvider.defaults.headers.common['Content-Type'] = 'application/json; charset=utf-8';

然后我尝试在工厂依赖项中添加$httpProvider

'use strict';
 angular.module('myApp')
.factory('authService', function ($q, $http, $rootScope, $window, httpProvider ) {
    return {
        authenticateUser: function () {
           ........
           ........
       $httpProvider.defaults.headers.common['Accept'] = 'application/json, text/javascript';
       $httpProvider.defaults.headers.common['Content-Type'] = 'application/json; charset=utf-8';

这次我收到了Unknown provider: $httpProviderProvider <- $httpProvider <- authService

请告诉我哪里出错了。

【问题讨论】:

    标签: angularjs angularjs-scope angularjs-service angularjs-factory


    【解决方案1】:

    $httpProvider 只能在像

    这样的配置中访问
    angular.module('myApp')
    .config(function($httpProvider){
       $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    })
    

    【讨论】:

    • 这个配置会应用于整个应用程序中的所有请求,还是我们需要为所有request使用interceptors
    • @Pradeep 是的,它将适用于所有请求 :)
    【解决方案2】:

    您无法在工厂内获得服务提供商。

    您可以使用interceptors为每个http请求添加一个默认值。

    angular.module('myApp').config(function ($httpProvider) {
        $httpProvider.interceptors.push('authInterceptorService');
    });
    
    angular.module('myApp').factory('authInterceptorService', ['$q', '$location', 'localStorageService', function ($q, $location, localStorageService) {
    var authInterceptorServiceFactory = {};
    var _authentication = {
        isAuth: false,
        userName: ""
    };
    
    var _request = function (config) {
        config.headers = config.headers || {};
        var authData = localStorageService.get('data');
        if (authData) {
            config.headers.Authorization = 'Bearer ' + authData;
        }
    
        return config;
    }
    
    authInterceptorServiceFactory.request = _request;
    
    return authInterceptorServiceFactory;
    }]);
    

    【讨论】:

    • 我在您的代码 sn-p 中看到了这一点:$httpProvider.interceptors.push('authInterceptorService'); 如果我有很多服务需要使用interceptors,该怎么办?
    • 一个问题。我在哪里定义了config?我得到configundefined
    • @Pradeep,你在问哪个configangular.module.config() 或名称为 config 的参数?
    • @Pradeep,当我们从 angularJS 发出 $http 请求时,我们的 interceptorsFactory.request() 将被触发。它应该将配置对象传递给request() 方法。
    • 知道了。感谢您让我知道 Angularjs 中的这个功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    相关资源
    最近更新 更多