自定义指令(如ng-repeat)

iLe*_*ing 8 angularjs ng-repeat

我已经尝试了很多不同的东西来解决性能问题ng-repeat.包括这里描述的内容:如何"观察"表达式

我需要在页面上有一大堆行,最多约1000行.每行包含相当多的东西.现在在我看来,它只会非常慢ng-repeat,我想我必须构建我自己的自定义ng-repeat或者我必须构建一个指令来构建表中的每一行...我不知道如何做其中任何一个.请你帮帮我吧 你能告诉我一些例子吗?

Noy*_*las 11

以下是使用<dt>和<dd>填充<dl>的示例...

步骤01 - 创建一个widge.product.details.js

//绑定到$ scope.details = [] //数组对象

angular.module('widget.product.details',[])
  .directive('productDetails',function(){
   return {
    template:'<dl class="dl-horizontal"></dl>',
    replace:true,
    restrict:'E',
    compile : function compile(tElement, tAttrs, transclude) {
     return {
      post: createProductDetails
     }
    }
   }
  });

var createProductDetails = function (scope, iElement, iAttrs, controller) {
    scope.$watch('details', function(newVal, oldVal) {
    angular.forEach(newVal, function(v,k){
        iElement.append( angular.element('<dt>'+v.dt+'</dt><dd>'+v.dd+'</dd>') );
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

第02步 - 创建你的HTML

<div class="span7" ng-controller="ProductInfoCtrl">
 <product-details></product-details>
</div>
Run Code Online (Sandbox Code Playgroud)

第03步 - 创建app.product.js

function ProductInfoCtrl($scope) {
 $scope.details = [
                   {dt:'condition',dd:'brand new'},
                   {dt:'year bought',dd:'3 years ago'},
                   ]
}
Run Code Online (Sandbox Code Playgroud)

  • 使用你的`forEach()`循环不是更好的性能,将它全部存储在一个字符串中然后在`forEach()`之后做一个`append()`? (2认同)

btm*_*tm1 5

angular.module('setRepeat',[]).directive('setRepeat', function () {

  return {
    transclude: 'element',
    priority: 1000,
    compile: compileFun
  };

  function compileFun(element, attrs, linker) {
      var expression = attrs.setRepeat.split(' in ');
      expression = {
        child : expression[0],
        property : expression[1]
      };

      return {
        post: repeat
      };

      function repeat(scope, iele, iattrs /*, attr*/) {
        var template = element[0].outerHTML;
        var data = scope.$eval(expression.property);
        addElements(data,scope,iele);

        return;

        function makeNewScope (index, expression, value, scope, collection) {
          var childScope = scope.$new();
          childScope[expression] = value;
          childScope.$index = index;
          childScope.$first = (index === 0);
          childScope.$last = (index === (collection.length - 1));
          childScope.$middle = !(childScope.$first || childScope.$last);

          /**
          *
          * uncomment this if you want your children to keep listening for changes
          *
          **/

          //childScope.$watch(function updateChildScopeItem(){
            //childScope[expression] = value;
          //});
          return childScope;
        }

        function addElements (collection, scope, insPoint) {
          var frag = document.createDocumentFragment();
          var newElements = [], element, idx, childScope;

          angular.forEach(data, function(v,i){
            childScope = makeNewScope(i,expression.child,v,scope,collection);
            element = linker(childScope, angular.noop);
            newElements.push(element);
            frag.appendChild(element[0]);
          });

          insPoint.after(frag);
          return newElements;
        }
      }
  }

});
Run Code Online (Sandbox Code Playgroud)