2013-04-10 60 views
1

我無法在控制器中獲得我的服務。這個項目在 最新版本中爲我創建模板並在構建時合併文件。獲取資源到控制器中

當改變角度剛停止工作,並沒有錯誤顯示在控制檯。

的錯誤信息是:

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

您正在向您的控制器注入UserServices,但隨後嘗試訪問UserService。看起來像一個簡單的錯字?

+0

哇,那是沒有的伎倆,謝謝你這麼多。如果你看起來很長,你顯然會錯過這個明顯的。我應該將此線程標記爲答案還是隻刪除它? – ArniReynir 2013-04-10 21:06:35

+1

@ArniReynir我會親自感謝接受的答案=) – jszobody 2013-04-10 21:07:21

0

在一個側面說明,我將放棄

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

,並只使用內置UserService.users。$保存() *編輯的錯字