我正在为我的项目使用 angular 1.6,并使用 angular-ui-routing 为 HTML 模板使用 PugJs 进行路由。 我正在尝试在我的应用程序中实现 Lazyload,但不知何故它不起作用可能是由于 jade。 代码:
var app = angular.module('myApp',['ui.router','oc.lazyLoad']);
app.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider
{
$ocLazyLoadProvider.config({
debug: true,
modules: [{
name: 'js',
files: ['js/*']
}]
});
}]);
.state("exampleState", {
url: '/example',
templateUrl: '/example',
controller:'exampleCtrl',
resolve: {
deps: ['$ocLazyLoad', function($ocLazyLoad) {
return $ocLazyLoad.load({
files: ['/js/exampleCtrl.js']
})
}]
}
})
Controller :
app.controller('exampleCtrl',function($scope){
console.log('controller loaded');
});
在前端,我使用 Node 将这些 Jade 转换为 HTML,因此当路由服务访问“templateUrl”时,它将被重定向到此代码:
app.get('/example', function(req, res) {
res.render('/example');
});
这会在 View 中加载 example.jade。 我在控制台中得到这个
[$controller:ctrlreg] The controller with the name 'exampleCtrl' is not registered.
即使 Controller 文件加载到 DOM 中, View 也不会呈现。欢迎任何有关问题的帮助。谢谢
最佳答案
经过多次搜索和尝试,我找到了解决方案,问题是构建 Controller 时模块的全局变量。而不是使用
app.controller('exampleCtrl',function($scope){
console.log('controller loaded');
});
我使用了 angular.module('myApp').controller(,,,);
关于javascript - Controller 已加载到 DOM 中,但 View 未加载且无法找到 Controller - oclazyload with jade(pugjs),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44160217/