teddy2sticks teddy2sticks - 1 month ago 5
AngularJS Question

How to use ng-click value as ng-repeat property?

How to get

$scope.clickedVal
in
r.clickedVal
?

Button click calls a function, passing in two parameters:

<a ng-click="display('Info', 'Admission_Info'); filters.Admission_Info= '!!'; ">Info</a>
<a ng-click="display('Info', 'SomeOther_Info'); filters.SomeOther_Info= '!!'; ">Other</a>


Function in the controller:

$scope.display = function (col, val) {
$scope.filters = {};
$scope.clickedColumn = col;
$scope.clickedVal = val;
};


Table displays a filtered view with 2 columns.

<tr ng-repeat="r in response | filter:filters as filtered " ng-show="filtered.length">
<td>{{ r.Name }}</td>
<td>{{ r.clickedVal }}</td>
</tr>


{{ r.{{clickedVal}} }}
didn't work.

So for example, if the first button is clicked,
r.clickedVal
should return
r.SomeOther_Info
.

I've tried creating a filter as well but run into the same issue -
m.value
is incorrect.

SchoolProgramsApp.filter('myFilter', function () {
return function (input, column, value) {
var out = [];
angular.forEach(input, function (m) {
if (m.value === value) {
out.push(m)
}
})
return out;
}
});

Answer

You should use bracket notation to access property of the object by variable name:

{{ r[clickedVal] }}