I've a module where I configure a route like this :
var app = angular.module("myModule", ['ui.grid','ui.bootstrap','ngRoute']);
app.config(function ($routeProvider) {
$routeProvider.when('/subjects', {
templateUrl: 'Subjects.aspx',
controller: 'SubjectsController'
})
}
In the template page I configure the controller like this :
<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>
In this way the controller is undefinied and seems that the scripts aren't loaded in the page correctly.
If I move the scripts in my Master Page (where I load the angular module) the controller is loaded correctly.
MasterPage :
<script src="/Scripts/SPScripts/Modules/Module.js"></script>
<div class="container body-content" data-ng-app="myModule">
<div data-ng-view></div>
</div>
I would like to load the various controllers on the pages where are needed so as not to burden the application, since they are loaded all into the master page.
**** - EDIT - ****
Seems that I can load scripts by using the 'resolve' in route :
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')
})
}
But I retrieve the error : $q is not defined.
Is it the correct way?