Javascript 尝试从WEB API加载数据时出错:[ng:areq]
我已经创建了一个角度模块,服务和控制器,它通过grunt缩小为一个文件。目录为scripts/profile/xxxxx.js,如下所示: 模块:Javascript 尝试从WEB API加载数据时出错:[ng:areq],javascript,angularjs,controller,asp.net-web-api2,Javascript,Angularjs,Controller,Asp.net Web Api2,我已经创建了一个角度模块,服务和控制器,它通过grunt缩小为一个文件。目录为scripts/profile/xxxxx.js,如下所示: 模块: (function () { 'use strict'; angular.module('profileModule', ['profileServices']); })(); (function () { 'use strict'; var profileServices = angular.module('profileServices', [
(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();
}
})();
[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"
}
};
}
}
服务:
(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();
}
})();
[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"
}
};
}
}
控制器:
(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();
}
})();
[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"
}
};
}
}
要缩小的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控制器:
(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();
}
})();
[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"
}
};
}
}
[路由(“api/[控制器]”)]
公共类配置文件控制器:控制器
{
//获取:api/值
[HttpGet]
公共IEnumerable Get()
{
返回新列表{
新配置文件
{
Id=1,
FirstName=“星球大战”,
LastName=“卢卡斯”,
电子邮件=”test@test.be"
},
新配置文件
{
Id=2,
FirstName=“金刚”,
LastName=“杰克逊”,
电子邮件=”test@test.be"
},
新配置文件
{
Id=3,
FirstName=“Memento”,
LastName=“Nolan”,
电子邮件=”test@test.be"
}
};
}
}
运行时,我不断收到一个错误:
[ng:areq]参数“profileController”不是函数,获取
未定义
这是否与缩小有关,或者问题是什么?我真的看不出来。刚开始使用AngularJS,欢迎您的任何意见 可能您忘了将controller js文件包含到index.html中
<script src="path_to_controller/profileController.js"></script>
I包含了缩小的javascript文件,该文件保存了脚本文件夹中的所有文件(通常控制器也是如此)。这可能是缩小的一个问题吗?你有没有尝试过使用未被缩小的版本来查看是否会出现相同的错误?然后就可以了。。所以问题是缩小。我会接受这个,因为它让我注意到它没有被适当缩小:)。虽然我已经包括了我的缩小JS。