我目前正在构建一个角度应用程序,它具有一个公开服务的C#后端。我想使用angularjs资源来访问这些服务。当我在控制器中调用资源时,我收到以下错误:
TypeError: Object function g(b){z(b||{},this)} has no method 'getCourse'
at new <anonymous> (http://127.0.0.1:81/ClientApp/controllers/CourseIndex.js:6:36)
at d (http://127.0.0.1:81/Scripts/angular/angular.min.js:30:352)
at Object.instantiate (http://127.0.0.1:81/Scripts/angular/angular.min.js:30:482)
at http://127.0.0.1:81/Scripts/angular/angular.min.js:59:495
at http://127.0.0.1:81/Scripts/angular/angular.min.js:47:450
at p (http://127.0.0.1:81/Scripts/angular/angular.min.js:7:367)
at U (http://127.0.0.1:81/Scripts/angular/angular.min.js:47:342)
at k (http://127.0.0.1:81/Scripts/angular/angular.min.js:42:309)
at k (http://127.0.0.1:81/Scripts/angular/angular.min.js:42:326)
at http://127.0.0.1:81/Scripts/angular/angular.min.js:41:371
这是我的控制器:
coursewareApp.controller('CourseIndex', function ($scope, $location, Courses) {
$scope.courseOutline = Courses.getCourse(); //Error in this line
$scope.courseOutline.then(
function(course) {
$scope.courseOutline = fixFormat(course);
},
function(response) {
console.log(response);
});
这是我的服务:
coursewareApp.factory('Courses', function ($resource, $q) {
var resource = $resource('/api/Block', {},
{
getCourse: {
method: 'GET',
headers: { 'Authorization-Token': '6A594741380806DF128E28BDCC072859A1ECF0DC24478DDD890CC191C8357AFF12F837F657104E285BE8E2151F8631D148DEF89420024EDAA71DDB8404A1C1947ABD2BBE8002952DC891246CADCB1D452F445E773AB0CE8B4B9CF566A738ED7736AFC66B61FBFDCD59EDD68D2F8F4D5FF31DA0DD8F0CA98C10CA07F18C1C386462D0D2694385DA38C9F6C2A04B15B885AEBDF917E155014942040272E0F36B2D4E39303CBC430738902CF75093EAD11492AA87A9' },
url: '/api/Block/Courses',
}
});
return {
getCourse: function () {
var deferred = $q.defer();
resource.getCourse(
function(event) {
deferred.resolve(event);
},
function(response) {
deferred.reject(response);
});
return deferred.promise;
}
};
});
我已经阅读了许多类似问题的解决方案,但似乎没有一个适用于此。
提前致谢。
答案 0 :(得分:2)
这似乎是一个缩小问题,尝试以缩小友好的方式声明控制器\服务
coursewareApp.controller('CourseIndex', ['$scope', '$location', 'Courses',function ($scope, $location, Courses) {
}]);
coursewareApp.factory('Courses', ['$resource','$q',function ($resource, $q) {
}]);