AngularJS + RequireJS + angular-ui-grid

AngularJS + RequireJS + angular-ui-grid

我正在尝试将 angular-ui-grid 与 AngularJS 和 RequireJS 一起使用。参见 plunker here

我的 index31.html 有网格并且 indexController.js 定义了 gridOptions 对象。需要时注入 indexController。

当浏览器在 index31.html 之前加载 indexController.js 时,它工作正常(即显示网格)但是当它反过来时,我得到错误:$scope.uiGrid is undefined.

如何指定(在 $stateProvider 配置或其他地方)总是在 index31.html 之前加载 indexController.js。或者,如何让所有控制器在 html 之前加载?

这样做的原因是您需要异步控制器的实际代码,即内联 require:

// app.js
define(function () {
    ...
    app.controller('IndexController', ['$scope', '$injector', function ($scope, $injector) {
        // HERE!!!
        require(['indexController'], function (controller) {
            $injector.invoke(controller, this, { '$scope': $scope });
        });
    }]);
});

无法保证此模式的加载顺序。

你能做什么:需要顶部的'indexController'

define(['indexController'], function (indexController) {
    ...
    app.controller('IndexController', indexController);
});

它甚至删除了 $injector!

的(可怕的 IMO)用法

(旁注:这样做,笨蛋抱怨indexController.js最后一行的$scope.$apply();我注释掉了,确实显得多余。)

笨蛋:http://plnkr.co/edit/fsyljR8FEeZdvXB3SRJP?p=preview