AngularJS $资源中的未知提供程序

时间:2017-05-04 09:25:40

标签: javascript angularjs angular-resource angular-factory

我收到了臭名昭着的未知提供商错误,并在这里查看了所有可能的答案,但我认为我错过了一些东西:

app.js

var myApp = angular.module('myApp', [
    'ngResource',
    'myAppControllers',
    'myAppServices',
    'myAppFilters'
]).config([
    '$locationProvider',
    function(
        $locationProvider) {

        $locationProvider.html5Mode({
            enabled: true,
            requireBase: false,
            rewriteLinks: false
        });
    }]);

var myAppControllers = angular.module('myAppControllers', []);

var myAppServices = angular.module('myAppServices', []);

var myAppFilters = angular.module('myAppFilters', []);

Item.js

myAppServices.factory('Item',
    function($resource) {
        return $resource('/api/items/:id');
    });

ItemService.js(1)

myAppServices.factory('itemService',
    function($q,
             $http,
             Item) {

        return {
            delete: function(id){

// Item, $q and $http are undefined here

                return Item.delete({id: id}).$promise;
            }
        };
    });

替代 ItemService.js(2)

myAppServices.factory('itemService', [
    '$q',
    '$http',
    'Item', // If I don't inject it here I can use this service with $q and $http
    function($q,
             $http,
             Item) {
        return {
            delete: function(id){
                return Item.delete(id).$promise;
            }
        };
    }]);

在我的控制器中:

myAppControllers.controller('ItemsCtrl', [
    '$scope',
    'itemService',
    function(
        $scope,
        itemService) {

        var ctrl = this;

        ctrl.ItemService = ItemService;

        ctrl.deleteItem = function(id){

            ctrl.itemService.delete(id)
                .then(function(response){
                    console.log(response);
                }, function (error) {
                    console.log(error);
            });

        };
    }]);
  1. 因此,如果我尝试(1),我会收到undefined.delete不是itemService中的函数。

  2. 如果我在(2)中尝试,则该应用无法加载:

    Unknown provider: ItemProvider <- Item <- itemService

  3. 那么我做错了什么?

2 个答案:

答案 0 :(得分:1)

这里有多个问题。

  • 您需要myAppServices作为myAppControllers的依赖项才能在控制器中使用它。

    所以,以下应该是你如何做到的:

    var myAppServices = angular.module('myAppServices', []);
    var myAppControllers = angular.module('myAppControllers', ['myAppServices']);
    
  • myAppServices模块中,您拥有使用Item的{​​{1}}服务,但您尚未将$resource作为ngResource的依赖项注入。

这是你的code in plunker没有错误

编辑:同时确保所有文件都正确包含在myAppServices中!

答案 1 :(得分:0)

我认为在$ resource中你缺少参数它看起来应该是

func getHeadingForDirection(fromCoordinate fromLoc: CLLocationCoordinate2D, toCoordinate toLoc: CLLocationCoordinate2D) -> Float {

        let fLat: Float = Float((fromLoc.latitude).degreesToRadians)
        let fLng: Float = Float((fromLoc.longitude).degreesToRadians)
        let tLat: Float = Float((toLoc.latitude).degreesToRadians)
        let tLng: Float = Float((toLoc.longitude).degreesToRadians)
        let degree: Float = (atan2(sin(tLng - fLng) * cos(tLat), cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(tLng - fLng))).radiansToDegrees
        if degree >= 0 {
            return degree
        }
        else {
            return 360 + degree
        }
    }

再次尝试(1),undefined是因为这个

并且你的删除应该看起来像这样

$resource('/api/items/:id', {id:'@id'});
相关问题