0

I am trying to use the variable i get from ng-repeat to accesses an object inside the scope.

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}};

3 Answers 3

1

Because block is a variable,so you cant use it as key, You need modify same as:

<td ng-repeat="block in blocks">{{ data[block].term }}</td>
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

0

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 }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.