简体   繁体   中英

AngularJS TypeError: $resource is not a function

I have the following controller that is giving me trouble in the form of

TypeError: $resource is not a function

pointing to var Activities = $resource('/api/activities');

app.controller('AddActivityCtrl',['$scope','$resource','$location','$rootScope',
    function($scope, $rootScope, $resource, $location){
        console.log($scope.user.email);
        //$scope.activity.user = $rootScope.user.email;
        $scope.save = function(){
            var Activities = $resource('/api/activities');
            $scope.activity.events = [];
            Activities.save($scope.activity, function(){
                $location.path('/')
            })
        }
    }
]);

It used to work well, but I just tested this feature and something I have done since I last tested it seems to have broken it. What could be the cause of this?

The following comes from app.js

var activities = require('./routes/activities');
app.use('/api/activities', activities);

And in my activities.js I have

//API point for listing all existing activities
router.get('/', function(req, res){
    var collection = db.get('Activity');
    //collection.find({}, function(err, activities){
    collection.find({user:req.user.email}, function(err, activities){
        if (err) throw err;
        res.json(activities);
    });
});

...

The issue seems to be with order of injection

app.controller('AddActivityCtrl',['$scope','$resource','$location','$rootScope',
    function($scope, $rootScope, $resource, $location){

You are injecting $resource as second service but passing $rootScope as second parameter.

Order of injection and arguments must match

['$scope','$resource','$location','$rootScope',
        function($scope, $resource, $location, $rootScope){..}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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