错误:[ng:areq]试图从WEB API加载数据

Error: [ng:areq] when trying to load data from WEB API

本文关键字:WEB API 加载 数据 ng areq 错误      更新时间:2023-09-26

我已经创建了一个Angular模块Service &使用grunt将这些控制器缩小为一个文件。目录为scripts/profile/xxxxx.js,如下所示:<<p> 模块/strong>:

(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 config to minify:

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 Controller:

    [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]参数'profileController'不是一个函数,得到未定义的

这与最小化有关还是有什么问题?我真的看不出来。刚开始使用AngularJS,所以任何输入都很感谢!

也许您忘记将控制器js文件包含到index.html

<script src="path_to_controller/profileController.js"></script>