問題如題。延遲加載進(jìn)controller文件后,controller提示不存在。
貼代碼:
state:
//state
$stateProvider.state('user.performance', {
url: '/performances',
views: {
'': {
controller: 'PerformanceCtrl',
templateUrl: 'tpl/user/performance'
}
},
resolve:{
loadPerformanceCtrl: ["$ocLazyLoad", function ($ocLazyLoad) {
return $ocLazyLoad.load('/app/performance/performance.js');//js文件有成功加載進(jìn)來
}]
}
})
performance.js
;(function () {
'use strict';
myApp.controller('PerformanceCtrl', [
'$scope',
'PerformanceService',
function ($scope, PerformanceService) {
console.log('PerformanceCtrl');
//...
])
})();
// myApp 在此前加載的js中已經(jīng)定義過。var myApp = angular.module('zwb', []);
報(bào)錯(cuò): Argument 'PerformanceCtrl' is not a
我搗鼓了下,發(fā)現(xiàn):
當(dāng)我創(chuàng)建一個(gè)新的module時(shí),$ocLazyLoad.load()
加載進(jìn)來的component registered成功,如下
angular.module('testtest', []).controller('PerformanceCtrl', [
//...
])
由上邊,我想到angular的另一個(gè)點(diǎn),當(dāng)module不帶參時(shí)是getter,所以嘗試如下:
angular.module('order').controller('PerformanceCtrl', [
//...
])
ocLazyLoad debug 提示ocLazyLoad.moduleReloaded order
,這樣perfomance下的component被load進(jìn)來,register也成功。
糾結(jié)了蠻久,對(duì)angular的理解以及引入組件的熟悉度還很不夠。感謝 G_Koala_C 的回答,thank you~
路由的resolve一般是預(yù)加載當(dāng)前state用到的數(shù)據(jù),數(shù)據(jù)加載完了之后再去加載頁面。你這個(gè)狀況,沒有必要懶加載呀,完全是畫蛇添足嗎。搞不清楚你這個(gè)錯(cuò)誤在哪里,大概兩種:
1,去掉懶加載,沒什么用。
2,將views中的controller改成loadPerformanceCtrl,resolve已經(jīng)直接重命名了。不確定好不好使。