简体   繁体   English

AngularJS中的动态ng-repeat

[英]Dynamic ng-repeat in AngularJS

My angular controller is 我的角度控制器是

$scope.dyna = [
    { "name": "parshuram", "age": 24 },
    { "name": "Tejash", "age": 26 },
    { "name": "Vinayak", "age": 25 }
];

My html 我的HTML

<table>
  <tbody>
    <tr ng-repeat="test in dyna">
     <td>{{test.name}}</td>
     <td>{{test.age}}</td>
    </tr>
  </tboody>
</table>

This works correctly, and outputs 这可以正常工作,并输出

Parshuram 24
Tejash    26

But if an another variable is added to my scope variable, I need to make changes in my html table: 但是,如果将另一个变量添加到我的范围变量中,则需要在html表中进行更改:

  $scope.dyna = [
       { "name": "parshuram", "age": 24 ,"void": true},
       { "name": "Tejash", "age": 26,"void" : false }
  ];

  <table>
      <tbody>
        <tr ng-repeat= "test in dyna">
         <td>{{test.name}}</td>
         <td>{{test.age}}</td>

         <!-- I don't want to have to add this, the columns should be added dynamically -->
        <td>{{test.void}}</td>
        </tr>
      </tboody>
    </table>

In that case, can the columns be generated dynamically, for example by getting all my object variables from the scope? 在那种情况下,是否可以动态生成列,例如通过从作用域获取我所有的对象变量?

ng-repeat can loop over object key/values as well : ng-repeat也可以遍历对象键/值

<table>
  <tbody>
    <tr ng-repeat= "test in dyna">
      <td ng-repeat="(key, value) in test">
        {{value}}
      </td>
     </tr>
  </tbody>
</table>

However as noted in the docs linked above, there are a few limitations compared to an ng-repeat that works on arrays: 但是,如上面链接的文档所述,与适用于数组的ng-repeat相比,存在一些限制:

  • The JavaScript specification does not define the order of keys returned for an object, so Angular relies on the order returned by the browser when running for key in myObj. JavaScript规范没有定义为对象返回的键的顺序,因此Angular依赖于在myObj中为键运行时浏览器返回的顺序。 Browsers generally follow the strategy of providing keys in the order in which they were defined, although there are exceptions when keys are deleted and reinstated. 浏览器通常遵循按定义顺序提供密钥的策略,尽管在删除和恢复密钥后会有例外。 See the MDN page on delete for more info. 有关更多信息,请参阅MDN页面上的delete。

  • ngRepeat will silently ignore object keys starting with $, because it's a prefix used by Angular for public ($) and private ($$) properties. ngRepeat会默默地忽略以$开头的对象键,因为它是Angular用于public($)和private($$)属性的前缀。

  • The built-in filters orderBy and filter do not work with objects, and will throw an error if used with one. 内置的过滤器orderBy和filter不适用于对象,如果与对象一起使用将引发错误。

You should be able to do that with (key,value) iteration. 您应该能够通过(键,值)迭代来做到这一点。

Would be nice to have fiddle to verify but would be something like: 有小提琴来验证会很好,但是会像这样:

<tr ng-repeat= "test in dyna">
    <td ng-repeat="(key,value) in test">{{value}}</td>
</tr>

If at 'runtime', I don't know. 如果在“运行时”,我不知道。 Otherwise: 除此以外:

<div ng-controller="MyCtrl">
  <table>
  <tbody>
    <tr ng-repeat= "test in dyna">
      <td ng-repeat="key in objectKeys(test)">{{test[key]}}</td>
    </tr>
  </tbody>
  </table>
</div>


var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.dyna = [
        { "name": "parshuram", "age": 24 },
        { "name": "Tejash", "age": 26 },
        { "name": "Vinayak", "age": 25 }
    ];

    $scope.objectKeys = function (object) {
      var keys = Object.keys(object);
      keys.splice(keys.indexOf('$$hashKey', 1))
      return keys;
    }

}

fiddle 小提琴

HTML HTML

<html>
<script src="library/angular.min.js"></script>
<script src="practice.js"></script>
<head>
</head>
<body ng-app="app">
<div ng-controller="test1" ng-bind-html="result">
</div>
</body>
</html>

Angularjs Angularjs

angular.module('app',[]).controller('test1',['$scope','$compile','$sce',function($scope,$compile,$sce){
    $scope.dyna = [
    { "name": "parshuram", "age": 24 },
    { "name": "Tejash", "age": 26 },
    { "name": "Vinayak", "age": 25 }
];
$scope.result="<table><tbody>";
for(var i=0;i<$scope.dyna.length;i++){
        $scope.result+="<tr>";
        for(var key in $scope.dyna[i])
            if($scope.dyna[i].hasOwnProperty(key))
                $scope.result+='<td>'+$scope.dyna[i][key]+'</td>';
        $scope.result+="</tr>";
}
    $scope.result+="</tbody></table>";
    $scope.result=$sce.trustAsHtml($scope.result);
}]);

This is another way, creating html in controller. 这是在控制器中创建html的另一种方法。

just make it again put another ng-repeat in your loop for the test variable: 只需再次使它在循环中为测试变量添加另一个ng-repeat即可:

$scope.dyna = [{ "name": "parshuram", "age": 24 ,"void": true}, { "name": "Tejash", "age": 26,"void" : false }];

  <table>
      <tbody>
        <tr ng-repeat= "test in dyna">

           <td ng-repeat="t in test">{{test[t]}</td> // just like this
        </tr>
      </tboody>
    </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM