Imagine you have an array of objects:
$scope.objArr = [{key:1,value:'value1'},{key:2,value:'value2'},{key:3,value:'value3'}];
And a variable that corresponds to key
. For instance:
$scope.a = 3;
In the Controller, you want to display the value
in the View. In this case, it should show "value3."
Although using ng-repeat
works, it may not feel right. Is there a better way to achieve this without creating another function?
UPDATE:
This is the complete code snippet being used:
<body ng-app="myApp">
<div ng-controller="MyController">
<span ng-repeat="obj in objArr | filter:{key:a.test}:true">{{obj.value}}</span>
<input ng-model="a.test"/>
</div>
</body>
Controller section:
var myApp = angular.module('myApp',[]);
myApp.controller('MyController',function($scope){
$scope.objArr = [{key:1,value:'value1'},{key:2,value:'value2'},{key:3,value:'value3'}];
$scope.a = {test:2}
});
Initially, it displays "value2", but it stops showing any value after changing the input field. What could be the reason for this?