我有以下控制器以
的形式给我带来麻烦TypeError:$ resource不是函数
指向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('/')
})
}
}
]);
它曾经运作良好,但我刚刚测试了这个功能以及我上次测试后所做的事情似乎已经打破了它。可能是什么原因造成的?
以下内容来自app.js
var activities = require('./routes/activities');
app.use('/api/activities', activities);
在我的活动中。我有
//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);
});
});
...
答案 0 :(得分:3)
问题似乎与注射顺序
有关app.controller('AddActivityCtrl',['$scope','$resource','$location','$rootScope',
function($scope, $rootScope, $resource, $location){
您正在注入$resource
作为第二项服务,但将$rootScope
作为第二个参数传递。
注射顺序和参数必须匹配
['$scope','$resource','$location','$rootScope',
function($scope, $resource, $location, $rootScope){..}