使用angularAMD时,ng-table未正确加载

时间:2015-04-23 17:39:04

标签: javascript angularjs requirejs angular-amd

我正在使用带有requirejs的angularAMD,当我尝试将ng-table模块提供给控制器时,我得到了Uncaught TypeError:无法从ng-table模块中读取未定义的属性“module”。错误的来源:

var app = angular.module('ngTable', []);

我的代码是:

'use strict';

requirejs.config({
  paths: {
    'angular': ['../lib/angularjs/angular'],
    'angular-route': ['../lib/angularjs/angular-route'],
    'angularAMD': ['../lib/angularAMD/angularAMD'],
    'ng-table': ['../lib/ng-table/ng-table']
  },
  shim: { 'angularAMD': ['angular'], 'angular-route': ['angular'], 'ng-table': ['angular']}
});


define(['angularAMD', 'angular-route'], function (angularAMD) {
    var app = angular.module("meadApp", ['ngRoute']);

    app.config(function ($routeProvider) {
        $routeProvider
            .when("/results", angularAMD.route({
                templateUrl: 'partials/results.html', controller: 'resultsController', controllerUrl: './controllers/resultsController'
            }))
            .otherwise({redirectTo: "/results"});
    });

    return angularAMD.bootstrap(app);
});

resultController.js

define(['app', 'ng-table'], function (app) {
    app.controller('resultsController', function ($scope, $http) {    
            $scope.users = [
                {name: "Moroni", age: 50},
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34}
            ];    
    });
});

results.html

<div>
    <table ng-table class="table">
        <tr ng-repeat="user in users">
            <td data-title="'Name'">{{user.name}}</td>
            <td data-title="'Age'">{{user.age}}</td>
        </tr>
    </table>
</div>

3 个答案:

答案 0 :(得分:2)

制作角度垫片和导出角度。像这样:

main.js:

requirejs.config({
  paths: {
    'angular': ['../lib/angularjs/angular'],
    'angular-route': ['../lib/angularjs/angular-route'],
    'angularAMD': ['../lib/angularAMD/angularAMD'],
    'ng-table': ['../lib/ng-table/ng-table']
  },
  shim: {
    'angular': { exports: 'angular' },
    'angularAMD': ['angular'],
    'angular-route': ['angular'],
    'ng-table': ['angular']}
});

我从这里发现了这个:https://github.com/esvit/ng-table/pull/377#issuecomment-57934733

@ marcoseu的回答对我不起作用。我发现我也不需要ngload用于Restangular,这是另一个第三方角度模块在角度被引导后加载。我不确定那是什么,我一定不明白。

答案 1 :(得分:1)

应使用ngload加载现有的角度模块。将ngload添加到main.js

requirejs.config({
  paths: {
    'ngload': ['../lib/angularAMD/ngload'],
    ...
  },
  shim: { 'ngload': ['angularAMD'], ...}
});

然后,使用它加载ng-table

define(['app', 'ngload!ng-table'], function (app) {
    app.controller('resultsController', function ($scope, $http) {    
            $scope.users = [
                {name: "Moroni", age: 50},
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34}
            ];    
    });
});

有关详细信息,请参阅3rd Party AngularJS Modules

答案 2 :(得分:-1)

如果在加载require.js配置之前加载了angular.js.然后,您需要在配置定义之前添加以下代码:

define('angular', function () {
    return angular;
});