Angularjs工厂注入不起作用

时间:2017-07-21 05:37:42

标签: javascript angularjs

我正在Angularjs开发一个Web应用程序。我正在使用$ http服务进行一些API调用。我正在使用工厂来拨打$ http服务。我有控制器以下。

(function () {
    angular.module('RoslpApp').controller('SavedVechilces',  ['$scope','DeleteVechicle',  function ($scope,DeleteVechicle)
 $scope.deletesavedCar = function (sref) {
            DeleteVechicle.deleteCar(sref.CarID).success(function (resposne) { console.log(resposne) }.error(function (error) { console.log(error)}))
        }
  }]);
})();


RoslpApp.factory("DeleteVechicle", ['$http', '$cookieStore','cfg', function ($http, $cookieStore,cfg) {
    var factoryObject = {};
    var baseurl = cfg.Baseurl;
    var LoginID = $cookieStore.get("LoginID");
    var cookiePreferredLanguage = $cookieStore.get('PreferredLanguage');
    var urlapi = baseurl + "api/Vehicle/DeleteSavedVehicle";
    factoryObject.deleteCar = function (carID) {
        var request = {
            url: urlapi,
            method: 'POST',
            data: {
                LoginID: LoginID,
                CarID: carID
            },
            headers: {
                RequestedPlatform: "Web",
                RequestedLanguage: cookiePreferredLanguage
            },
        };
        return $http(request);
    }
    return factoryObject;
}
]);

当我运行上面的代码时,我将获得未知提供程序:DeleteVechicleProvider< - DeleteVechicle< - SavedVechilce错误。我在我的控制器中注入了DeleteVechicle。我可以知道我对工厂的CarID方式并且返回响应是正确的方法吗?任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

有两个问题,您需要将控制器/工厂更改为

RoslpApp.controller

请求也应该是,

 DeleteVechicle.deleteCar(sref.CarID).then(function(response){ 
  console.log(resposne)
 },function(error){
   console.log(error)
 });