【发布时间】:2014-07-16 23:49:47
【问题描述】:
我正在使用资源调用服务器,当我转到的基本 URL 时
/viewEvent
它工作正常。我收到所有数据库条目。但是,当我去
/viewEvent/1234
其中 1234 是 eventID
我得到一个 undefined is not a function,这是 Angular 内部的崩溃。堆栈跟踪是
TypeError: undefined is not a function
at copy (http://localhost:8000/js/lib/angular/angular.js:593:21)
at http://localhost:8000/js/lib/angular/angular-resource.js:410:19
at wrappedCallback (http://localhost:8000/js/lib/angular/angular.js:6846:59)
at http://localhost:8000/js/lib/angular/angular.js:6883:26
at Object.Scope.$eval (http://localhost:8000/js/lib/angular/angular.js:8057:28)
at Object.Scope.$digest (http://localhost:8000/js/lib/angular/angular.js:7922:25)
at Object.Scope.$apply (http://localhost:8000/js/lib/angular/angular.js:8143:24)
at done (http://localhost:8000/js/lib/angular/angular.js:9170:20)
at completeRequest (http://localhost:8000/js/lib/angular/angular.js:9333:7)
at XMLHttpRequest.xhr.onreadystatechange (http://localhost:8000/js/lib/angular/angular.js:9303:11) angular.js:575
当我检查服务器时,请求是正确的。我可以看到它得到了 1234,它从 mongo 数据库中提取了正确的条目。
这是控制器逻辑
.controller("viewEventsController", ["$scope", 'EventService', '$location', function($scope, EventService, $location){
var path = $location.path().split('/');
var pathSize = path.length;
$scope.events = [];
if(pathSize === 2){
console.log("No event ID");
$scope.events = EventService.query();
}
else{
console.log("Event ID specified");
EventService.get({"eventID": path[pathSize - 1]}, function(data){
//$scope.events.push(data);
console.log(data);
}, function(error){
console.log(error);
});
}
}]);
和服务逻辑
service.factory('EventService', function($resource){
return $resource('api/viewEvent/:eventID');
});
它永远不会回到控制器,所以我“有信心”不是那样的。 (看着它)
【问题讨论】:
-
查看此答案以正确使用将参数传递到 $resource (eventID)。 stackoverflow.com/questions/19365714/…