How to load dynamic inline template with angular

狂风中的少年 提交于 2019-12-06 10:21:00

Directive:

app.directive('customtemp', function($templateCache, $compile) {
   return {  
       link: function(scope, element, attrs) {
            var z = $templateCache.get(scope.template);
            element.html(z);
            $compile(element.contents())(scope);
       }
   }
});

Main template:

<tr class="tableRowsDocs" ng-repeat="dbo in rows track by $index">
  <td class="tableColumnsDocs" ng-repeat="attobj in columns track by $index">
    <customtemp ng-init="values = dbo.get4(attobj.key); key = attobj.key; template = attobj.template || getAttributeTemplate(dbo.clazz + attobj.key);">

    </customtemp>
  </td>
</tr>

Example Loaded/called templates:

<script type="text/ng-template" id="links_as_dns_template">
      <div ng-repeat="dbo in values track by $index" ng-include="'link_as_dn_template'"></div>
</script>

<script type="text/ng-template" id="link_as_dn_template">
  <a href="#/view/{{ dbo.cid }}"><p>{{ dbo.displayName() }}</p></a>
</script>

Result 4 times faster than using ng-include.

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