gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:58:45 25 4
gpt4 key购买 nike

当我使用 $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,但 Angular 不会编译或其他什么,我只是得到带有字符串的括号。

我也收到此错误(请注意,我将 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)

最佳答案

好像在你的 Controller 中

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

您没有注入(inject)配置文件工厂。

关于javascript - AngularJS:服务错误,服务未定义。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33054865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com