【问题标题】:How to use JavaScript returned by AngularJS minified partials如何使用 AngularJS 返回的 JavaScript 缩小部分
【发布时间】:2015-08-13 20:03:52
【问题描述】:

我正在缩小我的 angularjs 应用程序,其中我使用 gulp-ng-html2js 来缩小部分,这看起来非常好,因为它将所有 html 部分转换为单个 javascript 文件,但是我不确定如何从我的应用程序中使用/调用这个 javascript 文件来访问部分,我试图找到某种文档,但网络上没有任何可用的文档;可能是我的搜索词或关键字不够准确。请在下面找到代码。请帮我弄清楚这一点,坦率地说,我相信我错过了一些基本的东西。

//gulp script for minification of html partials
...
....
....
gulp.task('htmlmin', function () {
    var opts = {
        empty: true,
        spare: true,
        quotes: true
    };

    gulp.src(paths.partialsSrc)
        .pipe(minifyhtml(opts))
        .pipe(ngHtml2Js({
            moduleName: "erentrollinternet",
            prefix: "/partials/"
        }))
        .pipe(concat("partials.min.js"))
        .pipe(uglify())
        .pipe(gulp.dest(paths.partialsDest));
});
....
....


// this is the resulting file
!function(t){
    try
    {
        t=angular.module("modulename")
    }
    catch(e)
    {
        t=angular.module("modulename",[])
    }
    t.run(["$templateCache",
    function(t)
    {
        t.put("/partials/components/building/Buildings.html",
        '<div class="main"><div class="controls">
            .....
            .....
            .....
          </div></div></div>')}])
    }(),
    function(t)
    {
        try{t=angular.module("erentrollinternet")
    }catch(e)
    {
        t=angular.module("erentrollinternet",[])
    }

}])}();

【问题讨论】:

  • 您是否在问除了在 HTML 脚本中包含 partials.min.js 之外还应该做什么?
  • 是的,你是对的,这是我的问题
  • 如果你打开 partials.min.js,你会看到它添加了那些在 $templateCache 中。所以你不需要做任何额外的事情来访问那些 html.. angular 会处理这些。

标签: angularjs gulp minify bundling-and-minification angularjs-view


【解决方案1】:

用于在$templateCache 中存储模板的键与文件仍在服务器上时使用的 url 相同。

每当您在路由、指令、ng-include 等中设置 templateUrl 时,angular 都会查看 $templateCache

如果在缓存中找到该 url,它将在内部将 html 从缓存对象中提取出来。如果没有找到,它将向模板发出服务器请求

简而言之,您不必在模块代码中做任何不同的事情,就可以按照现在缩小的方式使用它们。

【讨论】:

  • 感谢您提供的信息,我现在对那部分有所了解,但是因为我觉得它对我不起作用,所以想对此了解更多,我还阅读了 $templateCache 文档没有给出比你回答的更多的东西。我有以下目录结构 'project-> {appfolder and index.html}->components->account->accountPartial.html' 我在 templateUrl 和 min 文件中有相同的路径作为关键不知道为什么启动时应用程序无法找到部分文件
  • 任何脚本错误?有没有数据解析不出来?检查开发工具网络,看看那里是否有任何线索
  • 我确实检查过,除了找不到网络 404 文件外,我没有错误。你能否给我更多关于 min.js 文件中这个模块名称的信息,位置键似乎很好但我对模块名称有疑问,生成 min.js 时模块名称的意义是什么。在 Node.js 文件中,我确实尝试了保持模块名称与应用程序模块相同和不同的名称。只有我看到 URL 闪烁并且网络输出显示 404 文件未找到...
  • 我还有一个疑问,目前我的项目结构是这样的Project &gt; app.js &gt; index.html &gt; app &gt; &gt; components &gt; &gt; &gt; account (business modules) &gt; &gt; &gt; &gt; Account.html &gt;&gt; &gt; &gt; AccountController.js,对于这种结构,gulp 文档规定了不同的 gulp 脚本,但这也不起作用。
猜你喜欢
  • 1970-01-01
  • 2016-02-01
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 2014-01-29
  • 2023-01-13
相关资源
最近更新 更多