1

我无法在控制器中获取我的服务。这个项目在最新的 yeoman 下,它为我创建模板并在构建时合并文件。

更改某些内容时,角度会停止工作,并且控制台中不会显示任何错误。

错误信息是:

ReferenceError: UserService is not defined
at new <anonymous> (http://localhost:9000/scripts/controllers/people.js:5:25
at invoke (http://localhost:9000/components/angular/angular.js:2864:28)
at Object.instantiate (http://localhost:9000/components/angular/angular.js:2874:23)
at http://localhost:9000/components/angular/angular.js:4759:24
at <error: illegal access>
at Object.Scope.$broadcast (http://localhost:9000/components/angular/angular.js:8261:28)
at http://localhost:9000/components/angular/angular.js:7417:26
at wrappedCallback (http://localhost:9000/components/angular/angular.js:6797:59)
at wrappedCallback (http://localhost:9000/components/angular/angular.js:6797:59)
at http://localhost:9000/components/angular/angular.js:6834:26 

应用程序.js:

'use strict';

angular.module('jellybeanApp', ['jellybeanApp.Service'])
.config(function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);

$routeProvider
  .when('/', {
    templateUrl: 'views/main.html',
    controller: 'HomeController'
  })
  .when('/people', {
    templateUrl: 'views/people.html',
    controller: 'PeopleCtrl'
  })
  .otherwise({
    redirectTo: '/'
  });
 });

我的服务:

'use strict';

angular.module('jellybeanApp.Service', ['ngResource'])
  .factory('UserServices', function ($rootScope, $resource, $routeParams, $location) {
return {
  users : function() {
    return $resource('notimportant',
      {action: 'user', callback: 'JSON_CALLBACK'},
      {
        get: {method: 'JSONP', params: {UserId: $routeParams.UserId}},
        query: {method: 'JSONP'},
        create: {method: 'POST'}
      });
  }
};
});

控制器 :

'use strict';

angular.module('jellybeanApp')
 .controller('PeopleCtrl', function ($scope, UserServices) {
$scope.userResult = UserService.users().query();

});
4

2 回答 2

2

您正在注入UserServices您的控制器,但随后尝试访问UserService. 看起来像一个简单的错字?

于 2013-04-10T21:00:17.177 回答
0

在旁注中,我会放弃

  {action: 'user', callback: 'JSON_CALLBACK'},
  {
    ...
    create: {method: 'POST'}
  });

并且只使用内置的 UserService.users.$save() *编辑错字

于 2013-04-10T21:34:11.737 回答