AngularJS:服务错误,服务未定义。我究竟做错了什么?

时间:2015-10-10 14:08:14

标签: javascript angularjs

当我使用$ http服务时工作正常,但我想让它成为RESTful。我现在收到错误。有人能踢我正确的方向吗?我做错了什么?

app.js:

'use strict';

angular
    .module('swoleincApp', [
        'ngRoute',
        'ngSanitize',
        'ngAnimate',
        'ngResource'
    ])
    .config(['$routeProvider', '$locationProvider',
        function($routeProvider, $locationProvider) {
            $routeProvider
                .when('/models', {
                    templateUrl: 'app/views/main.html',
                    controller: 'MainCtrl'
                })
                .when('/models/:modelId', {
                    templateUrl: 'app/views/individual.html',
                    controller: 'ProfileCtrl'
                })
                .otherwise({
                    redirectTo: '/models'
                });

            $locationProvider.html5Mode(true);
        }]);

ProfileService.js:

'use strict';

angular
    .module('swoleincApp')
    .factory('Profile', ['$resource', ProfileService]);

function ProfileService($resource) {
    return $resource('app/storage/individual/:modelId.json', {}, {
        query: {
            method: 'GET', 
            params: {
                modelId: 'individual'
            }, 
            isArray: true
        }
    });
}

ProfileCtrl.js:

'use strict';

angular
    .module('swoleincApp')
    .controller('ProfileCtrl', ['$scope', ProfileCtrl]);

function ProfileCtrl($scope) {
    $scope.profile = Profile.query();
}

当我加载models /:modelId页面时,页面会加载到被点击的modelId,但是角度没有编译或者其他任何东西,我只是在其中得到包含字符串的括号。

我也得到了这个错误(注意我使用Laravel和Homestead和VirtualBox):

ReferenceError: Profile is not defined
    at new ProfileCtrl (http://laravel.app/app/js/controllers/ProfileCtrl.js:8:19)
    at e (http://laravel.app/app/js/dependencies/angular.min.js:39:193)
    at Object.instantiate (http://laravel.app/app/js/dependencies/angular.min.js:39:310)
    at http://laravel.app/app/js/dependencies/angular.min.js:80:313
    at A.link (http://laravel.app/app/js/dependencies/angular-route.min.js:7:268)
    at aa (http://laravel.app/app/js/dependencies/angular.min.js:73:90)
    at K (http://laravel.app/app/js/dependencies/angular.min.js:62:39)
    at g (http://laravel.app/app/js/dependencies/angular.min.js:54:410)
    at http://laravel.app/app/js/dependencies/angular.min.js:53:480
    at http://laravel.app/app/js/dependencies/angular.min.js:55:397 <div ng-view="" class="view-frame ng-scope" data-ng-animate="1">(anonymous function) @ angular.min.js:107
http://laravel.app/models/dom Failed to load resource: the server responded with a status of 404 (Not Found)

1 个答案:

答案 0 :(得分:3)

似乎在你的控制器中

function ProfileCtrl($scope) {
    $scope.profile = Profile.query();
}

你没有注入Profile工厂。