简体   繁体   English

ngInclude 不从 index.html 加载脚本 - AngularJS

[英]ngInclude not loading script from index.html - AngularJS

I'm starting a new AngularJS project using puikinsh/gentelella admin template.我正在使用puikinsh/gentelella管理模板开始​​一个新的 AngularJS 项目。

I extract the content section from index.html into 'dashboard' view and replace it with ngView.我将 index.html 中的内容部分提取到“仪表板”视图中,并将其替换为 ngView。 I extract the sidebar section into 'sidebar' view using ngInclude.我使用 ngInclude 将侧边栏部分提取到“侧边栏”视图中。

All the html successfully loaded but now the sidebar can't be clicked and also all the charts in 'dashboard' view is not loaded.所有 html 均已成功加载,但现在无法单击侧边栏,并且未加载“仪表板”视图中的所有图表。 I didn't change any script in the index.html except adding the angular script.除了添加 angular 脚本之外,我没有更改 index.html 中的任何脚本。

I tried to place the jQuery script before angular script, but still the issue persist.我试图将 jQuery 脚本放在 angular 脚本之前,但问题仍然存在。 The sidebar works fine and the charts loads too when I replace back the ngView & ngInclude with the html code from the view.当我用视图中的 html 代码替换 ngView 和 ngInclude 时,侧边栏工作正常,图表也会加载。

What am I missing?我错过了什么?

Extra:额外的:

My code: plnkr.co/edit/gOlvOgzvNBcu9cMHBzfd我的代码:plnkr.co/edit/gOlvOgzvNBcu9cMHBzfd

Getelella Admin Theme Demo: https://colorlib.com/polygon/gentelella/index.html Getelella 管理主题演示: https ://colorlib.com/polygon/gentelella/index.html

I didn't find an ng-app in your index.html.我在你的 index.html 中没有找到ng-app There is no entry point for Angular in your code.您的代码中没有 Angular 的入口点。 Check this sample Plunker.检查这个示例Plunker。 - https://embed.plnkr.co/nVCmukG5abpi1Y4ZHkrq - https://embed.plnkr.co/nVCmukG5abpi1Y4ZHkrq

I use vue.js's full rendering capability to code, also run into the case;我用vue.js的全渲染能力写代码,也碰到过这种情况;

I found that the reason is我发现原因是

<!-- Custom Theme Scripts -->
<script src="assets/theme/admin/gentelella/build/js/custom.min.js"></script>
<!--  vue.js components compiled file -->
<script src="{{ asset('js/app.js') }}"></script>

now I reverse their order,the sidebar's js is working with underside:现在我颠倒他们的顺序,侧边栏的 js 正在与下面一起工作:

<!--  vue.js components compiled file -->
<script src="{{ asset('js/app.js') }}"></script>
<!-- Custom Theme Scripts -->
<script src="assets/theme/admin/gentelella/build/js/custom.min.js"></script>

I think that you also require('....custom.min.js') in your js template, this can be load together with the template.我认为您的 js 模板中还需要 require('....custom.min.js') ,这可以与模板一起加载。

please remove the ng-app directive from html tag and place it on body tag.请从 html 标签中删除 ng-app 指令并将其放在 body 标签上。 you can see the output.你可以看到输出。

preview plunker is here: [example plunker][1]预览 plunker 在这里:[示例 plunker][1]

[1]: http://plnkr.co/edit/smCHpD3QK0zmfxkUT3CM?p=preview

I Managed to get it working by firstly moving the initial variable declarations in custom.js to a function.我设法通过首先将 custom.js 中的初始变量声明移动到函数来使其工作。

var CURRENT_URL = '',
    $BODY = '',
    $MENU_TOGGLE = '',
    $SIDEBAR_MENU = '',
    $SIDEBAR_FOOTER = '',
    $LEFT_COL = '',
    $RIGHT_COL = '',
    $NAV_MENU = '',
    $FOOTER = '';

function init_vars() {
    CURRENT_URL = window.location.href.split('#')[0].split('?')[0];
    $BODY = $('body');
    $MENU_TOGGLE = $('#menu_toggle');
    $SIDEBAR_MENU = $('#sidebar-menu');
    $SIDEBAR_FOOTER = $('.sidebar-footer');
    $LEFT_COL = $('.left_col');
    $RIGHT_COL = $('.right_col');
    $NAV_MENU = $('.nav_menu');
    $FOOTER = $('footer');
}

then inside your angular controller js file you need to add a run method that listens to the ngInclude '$includeContentLoaded' emitter.然后在您的角度控制器 js 文件中,您需要添加一个运行方法来侦听 ngInclude '$includeContentLoaded' 发射器。

in my case i am using an ng-include for the side-menu called menu.html在我的例子中,我使用了一个名为 menu.html 的侧边菜单的 ng-include

<div ng-include="'menu.html'" id="sidebar-menu" class="main_menu_side hidden-print main_menu"></div>

The 'menu.html' filename is what the second parameter (templateName) in the function will pick up. 'menu.html' 文件名是函数中的第二个参数 (templateName) 将选取的。

init_vars() is then called to define the global variables after the DOM has been loaded.然后调用 init_vars() 在 DOM 加载后定义全局变量。

We can then call the relevant inits based on what has been loaded as shown below.然后我们可以根据加载的内容调用相关的初始化,如下所示。

app.run(function ($rootScope) {
    $rootScope.$on("$includeContentLoaded", function (event, templateName) {
        init_vars(); //my defined function to load global variables after dom has been loaded
        if (templateName == 'menu.html') { //checking if this ng-include is the one that has loaded
            init_sidebar();
            //... other init functions
        }
    });
});

You will have to disable the call in custom.js that is triggered after the DOM has loaded that initializes everything.您必须禁用 custom.js 中的调用,该调用是在 DOM 加载完成初始化所有内容后触发的。 So the below code should be commented out in your custom.js file.所以下面的代码应该在你的 custom.js 文件中注释掉。

$(document).ready(function () {
    init_sparklines();
    init_flot_chart();
    init_sidebar();
    init_wysiwyg();
    //...other inits
});

If you run into issues with constant reloading just add a global variable that indicates whether the scripts have been loaded.如果您遇到不断重新加载的问题,只需添加一个全局变量,指示脚本是否已加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM