将资源转换为控制器

时间:2013-04-10 20:54:38

标签: angularjs yeoman

我在控制器内部获取服务时遇到了麻烦。这个项目是在 最新的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 

App.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();

});

2 个答案:

答案 0 :(得分:2)

您正在向控制器中注入UserServices,但之后尝试访问UserService。看起来像一个简单的拼写错误?

答案 1 :(得分:0)

另一方面,我会放弃

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

仅使用内置的UserService.users。$ save() *编辑错字