我有一個應用程序在angularjs每個控制器寫在不同的JS文件。AngularJS懶加載不工作
我只需要在routechange事件中調用這些文件。現在我成功地讓適當的控制器文件,但由於某種原因,其拋出的錯誤:
Error: [ng:areq] Argument 'ApplicantsController' is not a function, got undefined
http://errors.angularjs.org/1.2.25/ng/areq?p0=ApplicantsController&p1=not%20a%20function%2C%20got%20undefined
minErr/<@http://localhost/talentojo/app/js/angularjs/angular.js:78:5
[email protected]://localhost/talentojo/app/js/angularjs/angular.js:1509:5
[email protected]://localhost/talentojo/app/js/angularjs/angular.js:1520:76
$ControllerProvider/this.$get</<@http://localhost/talentojo/app/js/angularjs/angular.js:7278:9
nodeLinkFn/<@http://localhost/talentojo/app/js/angularjs/angular.js:6670:13
[email protected]://localhost/talentojo/app/js/angularjs/angular.js:332:11
[email protected]://localhost/talentojo/app/js/angularjs/angular.js:6657:11
[email protected]://localhost/talentojo/app/js/angularjs/angular.js:6105:13
[email protected]://localhost/talentojo/app/js/angularjs/angular.js:6001:30
z/<[email protected]://code.angularjs.org/1.2.25/angular-route.min.js:7:388
[email protected]://localhost/talentojo/app/js/angularjs/angular.js:6712:1
[email protected]://localhost/talentojo/app/js/angularjs/angular.js:6105:13
[email protected]://localhost/talentojo/app/js/angularjs/angular.js:6001:30
createBoundTranscludeFn/[email protected]://localhost/talentojo/app/js/angularjs/angular.js:6125:1
[email protected]://localhost/talentojo/app/js/angularjs/angular.js:6732:11
[email protected]://code.angularjs.org/1.2.25/angular-route.min.js:6:355
$RootScopeProvider/this.$get</[email protected]://localhost/talentojo/app/js/angularjs/angular.js:12980:15
l/<@https://code.angularjs.org/1.2.25/angular-route.min.js:11:127
qFactory/defer/deferred.promise.then/[email protected]://localhost/talentojo/app/js/angularjs/angular.js:11572:15
qFactory/defer/deferred.promise.then/[email protected]://localhost/talentojo/app/js/angularjs/angular.js:11572:15
qFactory/ref/<.then/<@http://localhost/talentojo/app/js/angularjs/angular.js:11658:11
$RootScopeProvider/this.$get</[email protected]://localhost/talentojo/app/js/angularjs/angular.js:12701:9
$RootScopeProvider/this.$get</[email protected]://localhost/talentojo/app/js/angularjs/angular.js:12513:15
$RootScopeProvider/this.$get</[email protected]://localhost/talentojo/app/js/angularjs/angular.js:12805:13
[email protected]://localhost/talentojo/app/js/angularjs/angular.js:8378:34
[email protected]://localhost/talentojo/app/js/angularjs/angular.js:8592:7
createHttpBackend/</[email protected]://localhost/talentojo/app/js/angularjs/angular.js:8535:1
我的代碼: HTML
<body>
<!-- Header Starts -->
<div ng-include="'assets/defaults/header.html'"></div>
<!-- Header Ends -->
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#applicants">Applicants</a>
</li>
</ul>
<div ng-view></div>
<!-- Footer Starts -->
<div ng-include="'assets/defaults/footer.html'"></div>
<!-- Footer Ends -->
</body>
路線:
var app = angular.module('TOJO',['ngRoute']);
app.config(function($routeProvider, $locationProvider) {
$routeProvider.when('/', {
templateUrl : 'assets/home.html',
controller : 'HomeController'
}).when('/applicants', {
templateUrl : 'assets/applicants/list.html',
scriptUrl : 'assets/applicants/applicants.js'
});
}).
run(function($rootScope, $location) {
$rootScope.$on("$routeChangeStart", function(event, next, current) {
if(next.scriptUrl !== undefined)
loadScript(next.scriptUrl);
});
});
app.controller('HomeController', function($scope) {
$scope.message = 'Look! I am home page.';
});
var loadScript = function(url, type, charset) {
if (type===undefined) type = 'text/javascript';
if (url) {
var script = document.querySelector("script[src*='"+url+"']");
if (!script) {
var heads = document.getElementsByTagName("head");
if (heads && heads.length) {
var head = heads[0];
if (head) {
script = document.createElement('script');
script.setAttribute('src', url);
script.setAttribute('type', type);
if (charset) script.setAttribute('charset', charset);
head.appendChild(script);
}
}
}
return script;
}
};
而文件applicants.js在路由變更時被呼叫:
app.controller('ApplicantsController',['$scope', function($scope) {
$scope.message = 'Look! I am home page.';
}
]);
list.html:
<div ng-controller="ApplicantsController">{{message}}</div>
我不認爲將角拿起新添加的腳本,一旦它運行 – thomaux 2014-09-23 06:48:58
它的加載腳本,我檢查了螢火蟲,但我thk由於某種原因,它首先加載模板,然後js文件,因爲它拋出該錯誤 – 2014-09-23 06:49:56
scriptUrl? WTF?這不是路由對象的公認屬性! https://docs.angularjs.org/api/ngRoute/provider/$routeProvider – Josep 2014-09-23 06:53:44