尝试从WEB API加载数据时出现错误:[ng:areq]

时间:2015-08-31 22:42:29

标签: javascript angularjs controller asp.net-web-api2

我创建了一个Angular Module,Service&控制器用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&#39; profileController&#39;得到了,不是一个功能   未定义

这是否与缩小或问题有关?我真的没有看到它。刚刚开始使用AngularJS,所以感谢任何输入!

1 个答案:

答案 0 :(得分:1)

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

<script src="path_to_controller/profileController.js"></script>
相关问题