How to load dynamic inline template with angular

2019-05-23 02:32发布

问题:

I have an ng-repeat that loops over an array of objects. At each loop the value 'template' & 'values' is initialised. The following version works and uses ng-include to load the template but it happens to be very very slow:

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

The value for template and values is dynamic but it always holds the id of a template/script like:

<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

Notice that the called template calles a second template also using ng-include.

I am trying to make things faster by using a custom directive to load the templates but cant seem to be able to make the following example to work in my case:

app.directive('ngInline', [
  '$templateCache',
  function($templateCache) {
    return {
      restrict: 'A',
      priority: 400, // Same as ng-include.
      compile: function(element, attrs){
        var templateName = attrs.ngInline;
        if(!templateName){
          throw new Error('ngInline: expected template name');
        }
        var template = $templateCache.get(templateName);
          if(angular.isUndefined(template)){
          throw new Error('ngInline: unknown template ' + templateName);
        }

        element.html(template);
      }
    };
  }
]);

Can anyone explain to me how to do this properly and efficiently(average 100 rows x35 columns ->multivalued cells/render)

This example is from: http://zachsnow.com/#!/blog/2014/angularjs-faster-ng-include/

回答1:

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.