【问题标题】:Using Grunt html2js and concat, how do I reference a html file from the concat js file使用 Grunt html2js 和 concat,如何从 concat js 文件中引用 html 文件
【发布时间】:2015-07-10 19:16:45
【问题描述】:

我有一个非常大的模块化 AngularJS 应用程序。我设置 Grunt 使用 html2js 从多个 html 文件创建单个 JS 文件。

    html2js: {
        options: {
            base: 'dol',
            module: 'dol.templates',
            singleModule: true,
            useStrict: true,
            htmlmin: {
                collapseBooleanAttributes: true,
                collapseWhitespace: true,
                removeAttributeQuotes: true,
                removeComments: true,
                removeEmptyAttributes: true,
                removeRedundantAttributes: true,
                removeScriptTypeAttributes: true,
                removeStyleLinkTypeAttributes: true
            }
        },
        main: {
            src: ['app/modules/reporting/views/*.html'],
            dest: 'tmp/templates.js'
        }
    },

然后我对我所有的 js 文件和新创建的 templates.js 文件运行 concat:

    concat: {
        options: {
            separator: ';'
        },

        dist: {
            src: ['tmp/*.js', 'app/app.js', 'app/app.controller.js', 'app/common/directives/directives.js', 'app/app.factory.js', 'app/modules/reporting/controllers/reports.controller.js', 'app/modules/reporting/reports.routes.js', 'app/xenon.custom.js'],
            dest: 'dist/app.js'
        }
    },

我现在有一个包含所有 js 文件和 html 文件的文件:app.js

我在 index.html 中引用了 dist/app.js 文件

我遇到的问题是在我的应用程序中我引用了一些 HTML 文件,例如:

// Layout Related Directives
directive('reportsSummary', function () {
    return {
        restrict: 'E',
        templateUrl: appHelper.templatePath('modules/reporting/views/reportssummary.view')
    };
}).
directive('reportsSidebarMenu', function () {
    return {
        restrict: 'E',
        templateUrl: appHelper.templatePath('modules/reporting/views/reports.sidebar.menu')
    };
}).
directive('reportsFooter', function () {
    return {
        restrict: 'E',
        templateUrl: appHelper.templatePath('modules/reporting/views/reports.footer')
    };
});

当我运行已部署的应用程序时,我收到一条错误消息,指出它找不到这些文件。当我在本地运行我的应用程序时,它运行良好,因为文件位于正确的路径中。

问题是:部署应用程序时,我如何在我的代码中引用这些 html 文件,因为它们现在位于 dist/app.js 中

我还引用了我的 ui-router 中的 html 文件: (功能 () { '使用严格';

angular
    .module('dol')
    .config(reportsConfig);

reportsConfig.$inject = ['$stateProvider', '$urlRouterProvider'];

function reportsConfig($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/reports');

    $stateProvider.state('reports',
        {
            url: '/reports',
            templateUrl: 'app/modules/reportspage/views/reports.view.html',
            controller: 'reportsController'
        });
}

})();

【问题讨论】:

标签: javascript angularjs gruntjs grunt-contrib-concat


【解决方案1】:

尝试在你的 grunt 任务中使用 ngtemplates:

https://www.npmjs.com/package/grunt-angular-templates

【讨论】:

  • 我不想缓存模板。此外,如果我将它们全部合并到一个 .js 文件中,我的 ui-router 将如何从新创建的 app.js 文件中访问该 html 文件?我的 ui-router 使用这个相对路径来查找 html 文件: templateUrl: 'app/modules/reportspage/views/reports.view.html' 现在我所有的 html 都在 dist/app.js 文件中合并为一个文件,我的 ui-router 如何知道查看 dist/app.js 文件与指定的相对路径?当我在本地运行它时,html文件都存在,但是当我在部署时组合html文件时,我使用dist/app.js。
  • 您为什么不想缓存模板?模板缓存本质上说“如果你正在寻找app/modules/reportspage/views/reports.view.html,这里就是”并返回 html 页面的正文。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
  • 1970-01-01
  • 2017-08-07
  • 1970-01-01
相关资源
最近更新 更多