简体   繁体   English

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

[英]AngularJS: Services error, service not defined. What am I doing wrong?

It was working fine when I used the $http service, but I wanted to make it RESTful. 当我使用$ http服务时,它工作正常,但我想使其成为RESTful。 I'm getting errors now. 我现在遇到错误。 Can someone kick me in the right direction? 有人可以向正确的方向踢我吗? What am I doing wrong? 我究竟做错了什么?

app.js: 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: 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: ProfileCtrl.js:

'use strict';

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

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

When I load the models/:modelId page, the page loads respective to the modelId that was clicked but the angular doesn't compile or whatever, I just get brackets with strings in them. 当我加载models /:modelId页面时,该页面分别加载到被单击的modelId上,但是没有编译角度或其他任何内容,我只得到带字符串的方括号。

I also get this error (note I am using Laravel with Homestead and VirtualBox): 我也收到此错误(请注意,我将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)

Seems like in your controller 好像在您的控制器中

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

you didn't inject Profile factory. 您没有注入Profile工厂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM