Angularjs 将重复表达式作为变量

Angularjs 将重复表达式作为变量,angularjs,expression,directive,ng-repeat,Angularjs,Expression,Directive,Ng Repeat,我正在尝试这样做: <ul> <li ng-repeat="{{myRepeatExpression}}">{{row.name}}</li> </ul> {{row.name} 但是由于ng repeat逻辑处于指令的编译状态,它将{{myRepeatExpression}}视为普通字符串而不是变量。这显然是行不通的 有什么解决办法吗 ng repeat只接受其专有表达式语法,如行中的行,但行可以是控制器中的函数或承诺。但是,您需

我正在尝试这样做:

<ul>
    <li ng-repeat="{{myRepeatExpression}}">{{row.name}}</li>
</ul>
  • {{row.name}
但是由于
ng repeat
逻辑处于指令的编译状态,它将
{{myRepeatExpression}}
视为普通字符串而不是变量。这显然是行不通的


有什么解决办法吗

ng repeat
只接受其专有表达式语法,如
行中的行
,但
可以是控制器中的函数或承诺。但是,您需要密切关注性能,因为ng repeat不能很好地处理经常更改的内容(可怕的最多10次迭代错误)。

您只能使用带有
ng repeat
的and表达式,而不能使用
插值的
值。
现在,为了创建动态可重复列表,您可以尝试以下任一方法:

  • 使用在
    ng repeat
    中动态返回列表的函数-这可能会更加昂贵,因为angular需要先调用该函数,然后在执行
    $digest
    循环时确定集合是否已更改
  • $watch
    对于作用域中触发列表更改的特定变量-可能更有效,但如果动态列表依赖于多个变量,则可能会变得更详细,并可能导致潜在的错误,因为在需要新变量时忘记添加新的
    $watch
  • JS:

    app.controller('MainCtrl', function($scope) {
      var values1 = [{name:'First'}, {name:'Second'}];
      var values2 = [{name:'Third'}, {name:'Fourth'}, {name:'Fifth'}];
    
      //1. function way
      $scope.getValues = function(id) {
        if(id === 1) {
          return values1;
        }
        if(id === 2) {
          return values2;
        }
      }
    
      //2. watch way
      $scope.values = undefined;
      $scope.$watch('id', function(newVal) {
        $scope.values = $scope.getValues(newVal);
      });
    });
    
    <!-- Here we pass the required value directly to the function -->
    <!-- this is not mandatory as you can use other scope variables and/or private variables -->
    <ul>
      <li ng-repeat="v in getValues(id)">{{v.name}}</li>
    </ul>
    <!-- Nothing special here, plain old ng-repeat -->
    <ul>
      <li ng-repeat="v in values">{{v.name}}</li>
    </ul>
    
    HTML:

    app.controller('MainCtrl', function($scope) {
      var values1 = [{name:'First'}, {name:'Second'}];
      var values2 = [{name:'Third'}, {name:'Fourth'}, {name:'Fifth'}];
    
      //1. function way
      $scope.getValues = function(id) {
        if(id === 1) {
          return values1;
        }
        if(id === 2) {
          return values2;
        }
      }
    
      //2. watch way
      $scope.values = undefined;
      $scope.$watch('id', function(newVal) {
        $scope.values = $scope.getValues(newVal);
      });
    });
    
    <!-- Here we pass the required value directly to the function -->
    <!-- this is not mandatory as you can use other scope variables and/or private variables -->
    <ul>
      <li ng-repeat="v in getValues(id)">{{v.name}}</li>
    </ul>
    <!-- Nothing special here, plain old ng-repeat -->
    <ul>
      <li ng-repeat="v in values">{{v.name}}</li>
    </ul>
    
    
    
    • {{v.name}
    • {{v.name}

    不能将ng repeat与应直接表示表达式的字符串/变量一起使用,但可以创建插入/解析此值的指令,并将其传递给ng repeat参数并重新编译元素

    app.directive('ngVarRepeat',function($compile){
      return {
        priority:1001, //must be higher than 1000 (priority of ng-repeat)
        compile:function($elm,$attrs){
    
          var expression = $attrs.ngVarRepeat;
          $elm.removeAttr('ng-var-repeat'); // remove attribute so we can recompile it later
    
          return function(scope,elm,attrs){
            $elm.attr('ng-repeat',scope.$eval(expression));
            $compile($elm)(scope);
          }
        }
      }
    })
    
    看看这个小玩意儿:


    另外请注意,这种方法会在嵌套的ng重复中造成问题。

    谢谢,我知道语法,但是如果要将表达式存储在变量中怎么办?如果它只是一个未计算的(DOM-)字符串(通过$parse或$scope.$eval),那么就没有办法这样做。