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

前端 未结 1 1732
后悔当初
后悔当初 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("<li>", model.data[i].name,"</li>");
        baz = baz.concat("<li>", model.data[i].desc.join().replace(/,/g,"</li><li>") );
        }
      
      $templateCache.put('name', bar);
      $templateCache.put('desc', baz);      
      }
    
    app.run(foo);
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="foo">
      <ul ng-include="'name'"></ul>
      <ul ng-include="'desc'"></ul>
    </div>

    References

    • Tweak the Angular Test by Controlling the Template

    • AngularJS in a Groovy World

    • AngularJS Source: templateRequestSpec.js

    • AngularJS Source: ngIncludeSpec.js

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