0

Controller:

$scope.item = {"name": "b", "code": 3}
$scope.foo = "name";

How to access {{item.name}} through 'foo'?

This doesn't work:

HTML:

{{item.{{foo}}}}

2 Answers 2

1

Try this template HTML:

{{item[foo]}}
Sign up to request clarification or add additional context in comments.

Comments

0

Use square brackets to access the value of variable as a key of object. You are nesting the angular expression {{}} within another which is incorrect to access the value of key of the object.

angular.module('myApp', []).controller('personCtrl', function($scope) {
   $scope.item = {"name": "b", "code": 3}
   $scope.foo = "name";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="personCtrl">

  <p>{{item[foo]}}</p>

</div>

2 Comments

He doesn't need item.foo, but item[foo] which is evaluated to item["name"].
@plvice oh yes. My bad

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.