简体   繁体   English

Angular ng-repeat变量,用于访问另一个作用域对象

[英]Angular ng-repeat variable to access inside another scope objects

I am trying to use the variable i get from ng-repeat to accesses an object inside the scope. 我正在尝试使用从ng-repeat获取的变量来访问范围内的对象。

My template is: 我的模板是:

<tr>
    <td style="font-weight: bold">term</td>
    <td ng-repeat="block in blocks">{{ data.block.term }}</td>
    <td>{{ total.term }}</td>
</tr>

My scope variables are: 我的范围变量是:

$scope.blocks = ['A', 'B', 'C'];
$scope.data = {'A': {'term': 0, 'term2': 7}, 'B': {'term': 2, 'term2': 3}, 'C': {'term': 5, 'term2': 14}};

由于block是一个变量,因此您不能将其用作键,您需要进行以下修改:

<td ng-repeat="block in blocks">{{ data[block].term }}</td>

 var app = angular.module("app",[]); app.controller("MyCtrl", function($scope){ $scope.blocks = ['A', 'B', 'C']; $scope.data = [{'term': 0, 'term2': 7}, {'term': 2, 'term2': 3}, {'term': 5, 'term2': 14}]; }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="MyCtrl"> <table> <tr> <td style="font-weight: bold">term</td> <td ng-repeat="block in blocks track by $index">{{ data[$index].term }}</td> </tr> </table> 

You cannot user this like that because data requires index you are not providing index block is not index in the case 您不能这样使用用户,因为数据需要索引,所以您不提供索引块就不是索引

You should use 你应该用

 {{ data[block.$index].term }}

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

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