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

落花浮王杯 提交于 2019-12-01 06:38:44

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!