Error: [ng:areq] when trying to load data from WEB API
Error: [ng:areq] when trying to load data from WEB API
我创建了一个 Angular 模块、服务和控制器,它们被缩小到一个文件中,带有 grunt。目录为scripts/profile/xxxxx.js 如下所示:
模块:
(function () {
'use strict';
angular.module('profileModule', ['profileServices']);
})();
服务:
(function () {
'use strict';
var profileServices = angular.module('profileServices', ['ngResource']);
profileServices.factory('Profiles', ['$resource',
function ($resource) {
return $resource('/api/profiles/', {}, {
query: { method: 'GET', params: {}, isArray: true }
});
}]);
})();
控制器:
(function () {
'use strict';
angular
.module('profileModule')
.controller('profileController', profileController);
profileController.$inject = ['$scope', 'Profiles'];
function profileController($scope, Profiles) {
$scope.profiles = Profiles.query();
}
})();
要缩小的 Grunt 配置:
module.exports = function (grunt) {
// load Grunt plugins from NPM
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
// configure plugins
grunt.initConfig({
uglify: {
my_target: {
files: {
'wwwroot/portal-min.js': [
'scripts/profile/profileModule.js',
'scripts/profile/profileController.js',
'scripts/profile/profileServices.js',
'scripts/**/*.js']
}
}
},
watch: {
scripts: {
files: ['scripts/**/*.js'],
tasks: ['uglify']
}
}
});
// define tasks
grunt.registerTask('default', ['uglify', 'watch']);
};
WEB API控制器:
[Route("api/[controller]")]
public class ProfilesController : Controller
{
// GET: api/values
[HttpGet]
public IEnumerable<Profile> Get()
{
return new List<Profile> {
new Profile
{
Id=1,
FirstName = "Star Wars",
LastName= "Lucas",
Email = "test@test.be"
},
new Profile
{
Id=2,
FirstName ="King Kong",
LastName ="Jackson",
Email = "test@test.be"
},
new Profile
{
Id =3,
FirstName ="Memento",
LastName ="Nolan",
Email = "test@test.be"
}
};
}
}
当 运行 时,我不断收到错误消息:
[ng:areq] Argument 'profileController' is not a function, got
undefined
这是否与缩小有关或有什么问题?我真的不明白。刚开始使用 AngularJS,欢迎任何意见!
可能是你忘了把控制器js文件包含到index.html
<script src="path_to_controller/profileController.js"></script>
我创建了一个 Angular 模块、服务和控制器,它们被缩小到一个文件中,带有 grunt。目录为scripts/profile/xxxxx.js 如下所示:
模块:
(function () {
'use strict';
angular.module('profileModule', ['profileServices']);
})();
服务:
(function () {
'use strict';
var profileServices = angular.module('profileServices', ['ngResource']);
profileServices.factory('Profiles', ['$resource',
function ($resource) {
return $resource('/api/profiles/', {}, {
query: { method: 'GET', params: {}, isArray: true }
});
}]);
})();
控制器:
(function () {
'use strict';
angular
.module('profileModule')
.controller('profileController', profileController);
profileController.$inject = ['$scope', 'Profiles'];
function profileController($scope, Profiles) {
$scope.profiles = Profiles.query();
}
})();
要缩小的 Grunt 配置:
module.exports = function (grunt) {
// load Grunt plugins from NPM
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
// configure plugins
grunt.initConfig({
uglify: {
my_target: {
files: {
'wwwroot/portal-min.js': [
'scripts/profile/profileModule.js',
'scripts/profile/profileController.js',
'scripts/profile/profileServices.js',
'scripts/**/*.js']
}
}
},
watch: {
scripts: {
files: ['scripts/**/*.js'],
tasks: ['uglify']
}
}
});
// define tasks
grunt.registerTask('default', ['uglify', 'watch']);
};
WEB API控制器:
[Route("api/[controller]")]
public class ProfilesController : Controller
{
// GET: api/values
[HttpGet]
public IEnumerable<Profile> Get()
{
return new List<Profile> {
new Profile
{
Id=1,
FirstName = "Star Wars",
LastName= "Lucas",
Email = "test@test.be"
},
new Profile
{
Id=2,
FirstName ="King Kong",
LastName ="Jackson",
Email = "test@test.be"
},
new Profile
{
Id =3,
FirstName ="Memento",
LastName ="Nolan",
Email = "test@test.be"
}
};
}
}
当 运行 时,我不断收到错误消息:
[ng:areq] Argument 'profileController' is not a function, got undefined
这是否与缩小有关或有什么问题?我真的不明白。刚开始使用 AngularJS,欢迎任何意见!
可能是你忘了把控制器js文件包含到index.html
<script src="path_to_controller/profileController.js"></script>