Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 向提供程序()和工厂()注册服务-在后者中找不到$http_Angularjs - Fatal编程技术网

Angularjs 向提供程序()和工厂()注册服务-在后者中找不到$http

Angularjs 向提供程序()和工厂()注册服务-在后者中找不到$http,angularjs,Angularjs,我正在创建一个包含一些服务的新模块。我按如下方式注册该服务: myModule.provider("surveySrv", ["$http", function ($http) { var httpSrv = $http; return { $get: function () { return { getall: function () { return httpSrv.get("/api/survey/all");

我正在创建一个包含一些服务的新模块。我按如下方式注册该服务:

myModule.provider("surveySrv", ["$http", function ($http) {
  var httpSrv = $http;
  return {
    $get: function () {
      return {
        getall: function () {
          return httpSrv.get("/api/survey/all");
        },
        remove: function (survey) {
          // ...
        }
      };
    }
  };
}]);
由于以下原因,我在实例化模块ilgServices时出错: 未知提供程序:$http

不知道为什么。 但如果我用工厂方法注册它,它会工作:

myModule.factory("surveySrv", ["$http", function ($http) {
    return {
      getall: function () {
      return $http.get("/api/survey/all");
    },
    remove: function (survey) {
      //return $http.get()
    }
  };
}])
我在GitHub上得到了这样的解释:

您只能将内容注入到提供者的$get属性中。$get属性是注入控制器/指令/服务的内容。提供程序的主体主要用于配置块来改变提供程序的行为

但是我有一个问题-如何“将内容注入$get属性”

在配置阶段调用
模块#provider
,此时只允许注入提供程序

您可以将服务注入$get函数,如下所示:

myModule.provider("surveySrv", function () {     
  return {
    $get: ["$http", function ($http) {
      return {
        getall: function () {
          return $http.get("/api/survey/all");
        },
        remove: function (survey) {
          // ...
        }
      };
    }]
  };
});