使用Grunt html2js和concat,我如何从concat js文件引用html文件

Using Grunt html2js and concat, how do I reference a html file from the concat js file

本文关键字:concat 文件 js html 引用 html2js Grunt 使用      更新时间:2023-09-26

我有一个非常大的模块化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

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

我遇到的问题是在我的应用程序中,我引用了一些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文件:(function () {使用严格的;

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

}) ();

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

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