通过加载控制器NG-包括不工作(Controller loaded by ng-include no

2019-10-21 06:02发布

我有一个网页弹出我在几个页面上使用,我用它加载NG-包括。 在此弹出式有三个选项卡也使用动态加载NG-包括。

所以,我的代码如下所示:

<div ng-include="'/pages/ng-templates/Cockpit.html'"></div>

Cockpit.html我有以下代码:

<div id="dlgCockpit" class="Cockpit jquerydialog">
    <div id="tabs">
        <ul>
            <li><a href="#tabA">tabA</a></li>
            <li><a href="#tabB">tabB</a></li>
            <li><a href="#tabC">tabC</a></li>
        </ul>

        <div id="tabA">
            <div ng-include="'/pages/ng-templates/Cockpit-A.html'"></div>
        </div>
        <div id="tabB">
            <div ng-include="'/pages/ng-templates/Cockpit-B.html'"></div>
        </div>
        <div id="tabC">
            <div ng-include="'/pages/ng-templates/Cockpit-C.html'"></div>
        </div>
    </div>
</div>

<script src="/pages/ng-templates/scripts/Cockpit.js"></script>

Cockpit-A.html我有以下代码:

<div id="dlgCockpitA">
    <form name="frmA" class="cntrCockpitA form" ng-controller="AController">
    <!-- some inputs using ng-model -->
    </form>
</div>
<script src="/pages/ng-templates/scripts/Cockpit-A.js"></script>

Cockpit.js

function showCockpit(vsTnID, vsBenutzer) {
    $('#dlgCockpit').data('tnid', vsTnID);
    var $dialog = $("#dlgCockpit")
        .dialog({
            autoOpen: false,
            title: locBenutzer + ': ' + vsBenutzer + ' (ID: ' + vsTnID + ')',
            width: 1000,
            show: 'fade',
            resizable: false,
            open: function (event, ui) {
              $("#tabs").tabs({ active: 0 });
              angular.element($('.cntrCockpitA')).scope().showStammdaten(vsTnID, 'Standard');
            },
            close: function (event, ui) {
            }
        });

    $dialog.dialog('open');
    return false;
}

Cockpit-A.js

app.controller('AController', ['$scope', '$http', function ($scope, $http) {
    //some function definitions on $scope
}]);

当网页加载完毕后,我得到的JavaScript控制台以下:

Error: [ng:areq] Argument 'AController' is not a function, got undefined
http://errors.angularjs.org/1.2.26/ng/areq?p0=StammdatenController&p1=not%20aNaNunction%2C%20got%20undefined
    at http://localhost:63323/scripts/angular/angular-1.2.26.js:78:12
    at assertArg (http://localhost:63323/scripts/angular/angular-1.2.26.js:1509:11)
    at assertArgFn (http://localhost:63323/scripts/angular/angular-1.2.26.js:1519:3)
    at http://localhost:63323/scripts/angular/angular-1.2.26.js:7278:9
    at http://localhost:63323/scripts/angular/angular-1.2.26.js:6670:34
    at forEach (http://localhost:63323/scripts/angular/angular-1.2.26.js:332:20)
    at nodeLinkFn (http://localhost:63323/scripts/angular/angular-1.2.26.js:6657:11)
    at compositeLinkFn (http://localhost:63323/scripts/angular/angular-1.2.26.js:6105:13)
    at compositeLinkFn (http://localhost:63323/scripts/angular/angular-1.2.26.js:6108:13)
    at publicLinkFn (http://localhost:63323/scripts/angular/angular-1.2.26.js:6001:30)

当调用showCockpit我得到的线以下错误angular.element($('.cntrCockpitA')).scope().showStammdaten(vsTnID, 'Standard');

Uncaught TypeError: undefined is not a function

其实一切正常,如果我把该脚本标签Cockpit-A.js在“基本页面”,它包括Cockpit.html或者,如果我只是通过调用创建控制器function AController($scope, $http) { ... } ,但是这两个是不是一种选择。

Answer 1:

我有同样的问题。 找到了解决办法感谢: http://ify.io/lazy-loading-in-angularjs/

基本上,这是不可能的自举程序后使用app.controller(...)。 我建议您阅读的链接以获取更多信息。 解决的办法是提前保存controllerProvider,并用它来注册模块。

var app = angular.module('myModule', []);
app.config(function($controllerProvider) {
    app.controllerProvider = $controllerProvider;
});

// Later on... after bootstrapping...
app.controllerProvider.register('myController', function() {... });


文章来源: Controller loaded by ng-include not working