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

ng-table not being loaded appropriately when using angularAMD

我将 angularAMD 与 requirejs 一起使用,当我尝试向控制器提供 ng-table 模块时,出现未捕获类型错误:无法读取 属性 'module'来自 ng-table 模块的未定义。错误来源:

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>

加载现有的 angular 模块应使用 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

做一个angular的垫片并导出angular。像这样:

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 的回答对我不起作用。我发现 Restangular 也不需要 ngload,这是在引导 angular 之后加载的另一个第 3 方 angular 模块。不知道是怎么回事,应该是没看懂。

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

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