AngularJS: Is there a better way to achieve this than the one specified?

前端 未结 1 1730
后悔当初
后悔当初 2021-01-13 08:52

I use a template that generates a Bootstrap tab layout. Like below:

1条回答
  •  醉梦人生
    2021-01-13 09:27

    Use ng-include with $templateCache rather than ng-repeat. For example:

    var app = angular.module('foo', []);
    
    function foo($templateCache)
      {
      var model = 
        {"data":
         [
           {"name": "Stack",
            "desc": ["Exchange", "Overflow"]
           }
         ]
        },
        cursor, i, bar = baz = "";
      
      for (i = 0; i < model.data.length; i++)
      	{
        bar = bar.concat("
  • ", model.data[i].name,"
  • "); baz = baz.concat("
  • ", model.data[i].desc.join().replace(/,/g,"
  • ") ); } $templateCache.put('name', bar); $templateCache.put('desc', baz); } app.run(foo);
  • 
    

        References

        • Tweak the Angular Test by Controlling the Template

        • AngularJS in a Groovy World

        • AngularJS Source: templateRequestSpec.js

        • AngularJS Source: ngIncludeSpec.js

        0 讨论(0)
      提交回复
      热议问题