Joe82 Joe82 - 5 months ago 15
AngularJS Question

AngularJS - calling a element in an array when I don't know its exact position

In angular, given a $scope like this


If I want to access one of the properties in the view I can do it like this


But if I don't know in advance what would be the order of the elements in the scope, would it be possible, for example, calling the property color of the object whose key is 1? Something like
but in an "Angular way". Thanks in advance!


Try this

angular.forEach($scope.colors, function(color) {
    if(color.key === 3) {
        $scope.selectedColor = color;

Then in the html view you can use this


If you want to do this in html side

<div ng-repeat="color in colors" ng-if="color.key===3">{{color.color}}</div>