Mapping a single element from an array in AngularJS without using ng-repeat

I have a JSON array like:

var data = [
{
  "name": "Jim",
  "age" : 25
},
{
  "name": "Jerry",
  "age": 27
}
];

in HTML:

<ul>
  <li ng-repeat="val in data | filter:{age: age}:true">{{val.name}}</li>
</ul>

The above code works fine, since I want to exit, but I don't want to use ng-repeatwhere it creates a loop, and then filters.

is there any other way where i can directly select DATA from array, where age = age??

+4
source share
4 answers

As mentioned in the @link comments, there is no way to tear an object from the required age from the array without scrolling it. However, you are correct that the use ng-repeathere is not suitable if you want to display only one object from an array, therefore, ideally, the required object should be stored in your controller.

$filter :

$scope.obj_with_correct_age = $filter("filter")(data, {age: age});

<li>{{ obj_with_correct_age.name }}</li>
+4

angular ( ngRepeat - ).
( JavaScript.)

. , (limitTo:1), 25 (filter:{age: 25}), JSON (json):

{{data | filter:{age: 25}:true | limitTo:1 | json}}

. .

+5

, , .

:

{{data.Systems[data.Detail.SystemId].System}}

, data.

+1

. { "name": "value" }. $http post , var data = response.

, console.log( [0].name); Jim

0
source

Source: https://habr.com/ru/post/1542018/


All Articles