javascript - 使用 gulp-angular-templatecache 将 HTML 合并到 template.js 中,出现 $injector Module Unavailable 错误

标签 javascript angularjs templates gulp angularjs-templates

我正在使用gulp-angular-templatecache ,获取所有部分 HTML 模板文件并将它们组合成 1 个 template.js 文件。

我能够完成的第一部分,我可以运行我的 Gulp 任务,它会创建 templates/templates.js

templates.js:

angular.module("templates").run(["$templateCache", function($templateCache) {$templateCache.put("beta.html","<div class=\"login-container\" ng-controller=\"BetaCtrl as beta\">\n    <section class=\"login-form-block\">\n        <header>\n            <div class=\"tickertags-logo-big\"></div>\n            <h1>Welcome to the TickerTags beta! Enter the email address where your beta invitation was sent below. We will send you an email with your temporary password.</h1>\n        </header>\n\n        <div class=\"login-form\">\n\n            <form name=\"loginForm\" ng-submit=\"beta.beta(credentials)\" novalidate>\n\n                <div class=\"login-alert\" ng-class=\"{ hideLoginMsg: beta.hideMessage == true }\">{{ beta.message }}</div>\n\n                <input type=\"email\"\n                       id=\"email\"\n                       placeholder=\"Email\"\n                       name=\"email\"\n                       ng-model=\"credentials.email\">\n\n                <button type=\"submit\" class=\"login-btn\">Email Password</button>\n            </form>\n\n            <p class=\"terms-of-use-line\">By signing up you agree to our <a href=\"assets/files/tickertags-terms-of-use.pdf\" target=\"_blank\">Terms of Use</a> and <a href=\"assets/files/privacypolicy.pdf\" target=\"_blank\">Privacy Policy</a></p>\n\n        </div>\n    </section>\n</div>\n");

现在我已在索引中正确链接到 templates.js 文件:

<script src="templates/templates.js"></script> <<--
<script src="authentication/authFactory.js"></script>
<script src="help/helpController.js"></script>
etc...

我已将其包含在主应用程序模块中:

var app = angular.module('tickertags', [
    'ui.router',
    'templates',  // <--
    'authFactory',
    'betaController',
    'passResetController',
    'loginController',
    'apiFactory',
    'scopeFactory',
    etc...

但仍然出现此错误: https://docs.angularjs.org/error/ $injector/nomod?p0=模板

Error: $injector:nomod Module Unavailable

关于为什么我会看到这个的想法?

最佳答案

因为该模块未正确声明。

您需要在应用程序中的某个位置声明模板模块,如下所示:angular.module("templates", []); 或使用options.standalone = true在 gulp 插件中

来自文档:

Note: this plugin will not create a new AngularJS module by default, but use a module called templates. If you would like to create a new module, set options.standalone to true.

关于javascript - 使用 gulp-angular-templatecache 将 HTML 合并到 template.js 中,出现 $injector Module Unavailable 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31438590/

相关文章:

javascript - 删除 DOM 并再次添加回来

javascript - Controller 中的 AngularJS 过滤器数组

javascript - 我应该如何管理 AngularJS 中不同环境的配置

c++ - typedef 到模板类型

javascript - AngularStrap typeahead,如何使用事件回调

php - 在 mySQL 中存储 javascript Date()

c++ - 模板的模板推导失败(中间有继承),有更好的方法吗?

templates - C++模板静态整数常量: out of class definition

javascript - 在网络浏览器中滥用伪造的用户输入

javascript - 想要使用 Angular 将表单中的数据作为 JSON 数组中的新对象提交