javascript - Use angular template cache with browserify - Stack Overflow

I am building a small angular app with browserify and ui-router. As I don't want to use a server,

I am building a small angular app with browserify and ui-router. As I don't want to use a server, I want to store all my templates using angular's $templateCache like this:

exports.templateCache = ["$templateCache", function($templateCache) {
  'use strict';

  $templateCache.put('partials/someState.html',
    "myHtmlCode"
  );
}];

To populate the cache, I use grunt to look into my partials folder, grab all the html and load it into the cache with grunt-angular-templates:

 ngtemplates:  {
  myApp: {
    cwd: 'dist/',
    src: 'partials/**.html',
    dest: 'src/js/templates/templates.js',
    options: {
      bootstrap:  function(module, script) {
        return 'exports.templateCache = ["$templateCache", function($templateCache) {\n' +
          script +
          '}];'
      }
    }
  }
},

I then use browersify to bine all my js together:

browserify: {
  dist: {
    files: {
      'dist/js/app.js': [
          'src/js/templates/**',
          'src/app.js'
          ],
    }
  }
},

This is working so far but this workflow looks very unwieldy to me: I have an intermediary step where I create the templates.js file in my src directory and I have hard-coded code in my grunt file.

Is there any way to do this more elegantly? Does browserify e with built in solutions to tackle this problem?

I am building a small angular app with browserify and ui-router. As I don't want to use a server, I want to store all my templates using angular's $templateCache like this:

exports.templateCache = ["$templateCache", function($templateCache) {
  'use strict';

  $templateCache.put('partials/someState.html',
    "myHtmlCode"
  );
}];

To populate the cache, I use grunt to look into my partials folder, grab all the html and load it into the cache with grunt-angular-templates:

 ngtemplates:  {
  myApp: {
    cwd: 'dist/',
    src: 'partials/**.html',
    dest: 'src/js/templates/templates.js',
    options: {
      bootstrap:  function(module, script) {
        return 'exports.templateCache = ["$templateCache", function($templateCache) {\n' +
          script +
          '}];'
      }
    }
  }
},

I then use browersify to bine all my js together:

browserify: {
  dist: {
    files: {
      'dist/js/app.js': [
          'src/js/templates/**',
          'src/app.js'
          ],
    }
  }
},

This is working so far but this workflow looks very unwieldy to me: I have an intermediary step where I create the templates.js file in my src directory and I have hard-coded code in my grunt file.

Is there any way to do this more elegantly? Does browserify e with built in solutions to tackle this problem?

Share Improve this question edited May 11, 2015 at 15:33 scniro 17k8 gold badges66 silver badges107 bronze badges asked Oct 22, 2014 at 7:54 SpearfisherSpearfisher 8,80324 gold badges74 silver badges126 bronze badges 1
  • Dealing with a similar issue. Have you found a solution yet? – Gabriel C. Troia Commented Nov 11, 2014 at 21:43
Add a ment  | 

2 Answers 2

Reset to default 3

browserify-ng-html2js has been designed to resolve this problem.

Simply add in package.json :

"browserify": {
    "transform": ["browserify-ng-html2js"]
 }

And you'll see if it walks the talks :)

Try transform for browserify that give you possibility to require html file (eg. Stringify). Then you can require('yourPartial.html') as string:

$templateCache.put('yourPartialId', require('./partials/yourPartial.html'));

// html file
<div ng-include=" 'yourPartialId' "></div>    

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745316667a4622250.html

相关推荐

  • javascript - Use angular template cache with browserify - Stack Overflow

    I am building a small angular app with browserify and ui-router. As I don't want to use a server,

    12小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信