简体   繁体   English

Angular中的未知$ http提供程序

[英]Unknown $http Provider in Angular

I need to use $http service in config method. 我需要在config方法中使用$http服务。 But I can't use $http in config, I am getting unknown Provider error message. 但我不能在配置中使用$http ,我收到unknown Provider错误消息。

My code : 我的代码:

.config(function($http, $routeProvider, $provide) {
    $http.get("sampleslist.js").success(function(data) {
        var loop = 0, currentRoute;

        for (loop = 0; loop < data[loop].pages.length; loop++) {
            currentRoute = data[loop].pages;

            var routeName = "/" + currentRoute[loop].name;
            $routeProvider.when(routeName, {
                templateUrl:"/" + currentRoute.name + ".html",
            })
        }
    })

    app = {controller: $controllerProvider.register}
})

Can you please provide the solution for this? 你能为此提供解决方案吗?

It's not a good practice to use $http in the .config . .config使用$http不是一个好习惯。 One can use it either in .run or can make use of the service using either .service or .factory . 可以在.run使用它,也可以使用.service.factory来使用服务。

If just need to make AJAX request you can actually retrieve $http server from the new injector: 如果只需要发出AJAX请求,您实际上可以从新的注入器中检索$ http服务器:

.config(function($routeProvider, $provide) {
    var $http = angular.injector(['ng']).get('$http');
    $http.get("sampleslist.js").success(

        function(data) {

            var loop = 0,
                currentRoute;

            for (loop = 0; loop < data[loop].pages.length; loop++) {

                currentRoute = data[loop].pages;

                var routeName = "/" + currentRoute[loop].name;

                $routeProvider.when(routeName, {

                    templateUrl: "/" + currentRoute.name + ".html",

                })
            }
        })

    app = {
        controller: $controllerProvider.register,
    }
})

We cannot use http in config. 我们不能在config中使用http。 For more details please check this link: 有关详细信息,请查看此链接:

use $http inside custom provider in app config, angular.js 在app config,angular.js中使用自定义提供程序中的$ http

You can inject only providers in .config file. 您只能在.config文件中注入提供程序。 but $http is not provider, your angular app is searching for provider named $http but it is not able to find it so it threw error. 但$ http不是提供者,你的角度应用程序正在搜索名为$ http的提供程序,但它无法找到它,因此它引发了错误。

How to resolve this?? 怎么解决这个?

If you want any ajax call before your angular controller loading you can do it in .run 如果你想在角度控制器加载之前调用任何ajax,你可以在.run中进行

Do you have control over sampleslist.js ? 你有控制sampleslist.js吗? If yes, maybe just download it using tag and assign values to variables. 如果是,也许只需使用标记下载它并为变量赋值。

//sampleslist.js
var samplelist = {...}

//index.html
<script src="samplelist.js"></script>

Then you could inject this values to angular using angular.constant or just use variable samplelist 然后,您可以使用angular.constant将此值注入角度,或者只使用变量samplelist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM