【问题标题】:Controller is loaded in DOM but the view not loaded and can't find controller- oclazyload with jade(pugjs)控制器已在 DOM 中加载,但视图未加载且无法找到控制器 - oclazyload with jam(pugjs)
【发布时间】:2017-10-24 21:45:15
【问题描述】:

我在我的项目中使用 angular 1.6,并使用 angular-ui-routing 使用 PugJs 为 HTML 模板进行路由。 我正在尝试在我的应用程序中实现 Lazyload,但不知何故它不工作可能是由于玉。 代码:

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']
                })
            }]
        }
    })

控制器:

app.controller('exampleCtrl',function($scope){
  console.log('controller loaded');
});

在前端,我使用节点将这些翡翠转换为 HTML,因此当路由服务访问“templateUrl”时,它将被重定向到以下代码:

app.get('/example', function(req, res) {
    res.render('/example');
});

这会在视图中加载 example.jade。 我在控制台中得到这个

[$controller:ctrlreg] 名为“exampleCtrl”的控制器未注册。

即使在 DOM 中加载了控制器文件并且视图也不会呈现。欢迎任何有关问题的帮助。谢谢

【问题讨论】:

  • 您能否使用最少的代码创建 StackSnippet/Plnkr/JSFiddle 来重现该问题。
  • 当然@Tushar 会在一段时间内更新!!
  • 很难从提供的代码中分辨出来,但请确保您的解决方案正在返回一个承诺。如果您完全删除解析,也许看看它是否呈现。

标签: javascript angularjs node.js pug oclazyload


【解决方案1】:

我认为你应该这样做有点不同。试试这个:

$stateProvider.state('exampleState', {
  url: "/example",
  views: {
    "exampleLazyLoadView": {
      controller: 'exampleCtrl',
      templateUrl: '/example.html'
    }
  },
  resolve: { // Any property in resolve should return a promise and is executed before the view is loaded
    loadMyCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
             return $ocLazyLoad.load('/js/exampleCtrl.js');
    }]
  }
});

【讨论】:

  • 提供所有代码烧烤这个作品。我认为您在其他地方有问题(仔细检查路径)。我不确定files: ['js/*'] 是否是有效声明。
【解决方案2】:

经过太多的搜索和尝试我找到了解决方案,问题是构建控制器时模块的全局变量。而不是使用

app.controller('exampleCtrl',function($scope){
  console.log('controller loaded');
});

我用angular.module('myApp').controller(,,,);

参考:ocLazyLoad Issues

【讨论】:

    猜你喜欢
    • 2016-03-28
    • 2014-08-24
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 2019-11-09
    • 2018-04-16
    相关资源
    最近更新 更多