Skip to content Skip to sidebar Skip to footer

Speeding Up Angular $compile Function

I'm manually compiling a template against a new scope: var scope = _.assign($rootScope.$new(true), { foo: 1, bar: 2 }) var element = angular.element('

Solution 1:

If the elements are of the same structure and only differ in the scope against which they are linked, then you should $compile the template once and then link each of the 100 elements against their respective scopes.

var myElement = angular.element('<my-element foo="foo" bar="bar"></my-element>');
var myElementLinkFn = $compile(myElement);


// "items" here is the data that drives the creation of myElement elements
angular.forEach(items, function(item){
   var newScope = $scope.$new(true);

   newScope.foo = item.foo;
   newScope.bar = item.bar;

   myElementLinkFn(newScope, function(clone){
      container.append(clone);
   })
});

Post a Comment for "Speeding Up Angular $compile Function"