简体   繁体   English

angular:使用变量键访问JSON值

[英]angular: accessing a JSON value using a variable key

I have a variable param in scope as $scope.param that is always either foo or bar . 我在范围内有一个变量param作为$scope.param ,它总是foobar I'm creating a table with rows defined by <tr ng-repeat="d in data"> . 我正在创建一个表,其中的行由<tr ng-repeat="d in data">

When I have the following: <td>{{d.foo}}</td> or <td>{{d.bar}}</td> everything works and the data shows up fine. 当我有以下内容时: <td>{{d.foo}}</td><td>{{d.bar}}</td>一切正常,数据显示正常。 However, when I have <td>{{d.param}}</td> angular can't find anything and the cell is blank. 但是,当我有<td>{{d.param}}</td> angular时,找不到任何内容并且单元格为空。

In other words, I'm trying to access an object value using a variable as the key rather than the key itself. 换句话说,我试图使用变量作为键而不是键本身来访问对象值。 Any idea how to do this? 知道怎么做吗?

使用括号表示法:

{{d[param]}}

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

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