0

我有一个模块,我在其中配置这样的路由:

var app = angular.module("myModule", ['ui.grid','ui.bootstrap','ngRoute']);
app.config(function ($routeProvider) {
        $routeProvider.when('/subjects', {
            templateUrl: 'Subjects.aspx',
            controller: 'SubjectsController'
        })
    }

在模板页面中,我像这样配置控制器:

<script src="/Scripts/SPScripts/Services/SubjectService.js"></script>
<script src="/Scripts/SPScripts/Controllers/SubjectController.js"></script>

        <div id="dvcollection" data-ng-controller="SubjectController">
            <div style="padding-left: 15px; padding-bottom: 10px;">
                <button type="button" id="addRow" class="btn btn-success" data-ng-click="addRow()">Nuovo</button>
            </div>
            <div class="gridStyle" data-ui-grid="gridOptions"></div>
        </div>

通过这种方式,控制器是未定义的,并且似乎脚本没有正确加载到页面中。

如果我在我的母版页(我加载角度模块的地方)中移动脚本,控制器就会正确加载。

母版页:

<script src="/Scripts/SPScripts/Modules/Module.js"></script>
<div class="container body-content"  data-ng-app="myModule">
            <div data-ng-view></div>
        </div>

我想在需要的页面上加载各种控制器,以免给应用程序带来负担,因为它们全部加载到母版页中。

**** - 编辑 - ****

似乎我可以通过在 route 中使用“resolve”来加载脚本:

var resolveController = function (path) {
  var deferred = $q.defer();
  var script = document.createElement('script');
  script.src = path;
  script.onload = function () {
    $scope.$apply(deferred.resolve());
  };
  document.body.appendChild(script);
  return deferred.promise;
};

app.config(function ($routeProvider) {
        $routeProvider.when('/subjects', {
            templateUrl: 'Subjects.aspx',
            controller: 'SubjectsController',
            resolve: resolveController('/Scripts/SubjectController.js')
        })
    }

但我检索到错误:$q 未定义。

这是正确的方法吗?

4

4 回答 4

0

试试这个:从模板中删除 data-ng-controller="SubjectController"

于 2016-02-29T10:45:23.947 回答
0

after your edit it seems you didn't 'inject' $q in your resolver :

var resolveController = function ($q,$rootscope) {
  var deferred = $q.defer();
  var script = document.createElement('script');
  script.src = '/Scripts/SubjectController.js';
  script.onload = function () {
    $rootscope.$apply(deferred.resolve());
  };
  document.body.appendChild(script);
  return deferred.promise;
};

app.config(function ($routeProvider) {
    $routeProvider.when('/subjects', {
        templateUrl: 'Subjects.aspx',
        controller: 'SubjectsController',
        resolve: { resolveController: resolveController
    })
}

I've replaced $scope with $rootscope and injected it as $scope isn't defined.

Hope this help, found this question on the subject with a interesting discussion about it.

于 2016-03-03T11:00:14.287 回答
0

在您的 Angular 模块文件中:

var app = angular.module("myModule", ['ui.grid','ui.bootstrap','ngRoute']);
app.config([ $routeProvider , function ($routeProvider) {
        $routeProvider.when('/subjects', {
            templateUrl: 'Subjects.aspx',
            controller: 'SubjectsController'
        });
    }])

然后在您的模板文件中

<div id="dvcollection" >
            <div style="padding-left: 15px; padding-bottom: 10px;">
                <button type="button" id="addRow" class="btn btn-success" data-ng-click="addRow()">Nuovo</button>
            </div>
            <div class="gridStyle" data-ui-grid="gridOptions"></div>
        </div>

在您的母版页中

<script src="/Scripts/SPScripts/Modules/Module.js"></script>
<div class="container body-content"  data-ng-app="myModule">
        <div data-ng-view></div>
</div>
于 2016-02-29T10:51:00.630 回答
0

你需要这样做:

$injector = angular.injector(['ng']);
q = $injector.get('$q');
var deferred = q.defer();
于 2016-08-02T10:27:44.803 回答