Javascript 如何在Angular JS ng-repeat中访问数组中的第一个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32487418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:30:55  来源:igfitidea点击:

How to access first value from array in Angular JS ng-repeat

javascriptarraysangularjs

提问by Emamul Andalib

This is the array

这是数组

["236.jpg","239.jpg","294.jpg","748.jpg","157.jpg","446.jpg","871.jpg","778.jpg"]

I want to access

我想访问

"236.jpg"

“236.jpg”

. The code right below which I am using to fetch the top array.Now how can I fetch the first item using below code ?

. 下面是我用来获取顶部数组的代码。现在如何使用下面的代码获取第一项?

<tr ng-repeat="x in p">
    <td>
     {{ x.images }}
    </td>
</tr>

Please help me to find out the sollution.

请帮我找出解决方案。

For more here is the full code

有关更多信息,请参阅完整代码

{"info":[{"id":"11","name":"brown","description":"fasdfasd","size":"fasdf","color":"5a72fb","created_at":"2015-09-08 22:33:33","updated_at":"2015-09-08 22:33:33","images":"[\"236.jpg\",\"239.jpg\",\"294.jpg\",\"748.jpg\",\"157.jpg\",\"446.jpg\",\"871.jpg\",\"778.jpg\"]"},{"id":"13","name":"fasdf","description":"asdfghjkl","size":"fasdf","color":"5a72fb","created_at":"2015-09-09 11:48:31","updated_at":"2015-09-09 11:48:31","images":"[\"910.jpg\",\"504.jpg\",\"784.jpg\"]"}]}


angular.module('myApp', []).controller('myCtrl', function($scope, $http){
      $http.get('test').success(function (data){
        $scope.p = angular.fromJson(data);
        console.log(angular.fromJson(data));
      });
});

<tbody ng-controller="myCtrl">
   <tr ng-repeat="x in p">
    <td ng-if="x.images ">
        {{ x.images | limitTo:$index }}
    </td>
    <td>{{ x.size }}</td>
   </tr>
</tbody>

Now please help me with full code. Thank you.

现在请帮助我提供完整的代码。谢谢你。

回答by kjonsson

Well just doing the following will work:

那么只需执行以下操作即可:

<td>
    {{p[0]}}
</td>

回答by Ritesh

There are many ways to do it. One way would be to use filterlimitTo:1 with ng-repeat.

有很多方法可以做到。一种方法是将过滤器limitTo:1 与 ng-repeat 一起使用。

Try this

尝试这个

<tr ng-repeat="x in p | |limitTo:1">
    <td>
     {{ x.images }}
    </td>
</tr>

回答by Thinker

For your condition, you can use ng-ifinside ng-repeatas Pankaj said in the comment.

对于您的情况,您可以像 Pankaj 在评论中所说的那样使用ng-ifinside ng-repeat

  var app = angular.module('ExampleApp', []);
  app.controller('appController', ['$scope', function($scope) {
    $scope.p = {
      "info": [{
        "id": "11",
        "name": "brown",
        "description": "fasdfasd",
        "size": "fasdf",
        "color": "5a72fb",
        "created_at": "2015-09-08 22:33:33",
        "updated_at": "2015-09-08 22:33:33",
        "images": ['\"236.jpg\"','\"504.jpg\"','\"784.jpg\"']
      }, {
        "id": "13",
        "name": "fasdf",
        "description": "asdfghjkl",
        "size": "fasdf",
        "color": "5a72fb",
        "created_at": "2015-09-09 11:48:31",
        "updated_at": "2015-09-09 11:48:31",
        "images": ['\"910.jpg\"','\"504.jpg\"','\"784.jpg\"']
      }]
    }

  }]);
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.js"></script>
  <script src="http://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
  <script src="script.js"></script>
</head>

<body ng-app="ExampleApp">
  <div ng-controller="appController">
<div ng-repeat="x in p.info">
  {{ x.images[0] +" are first"}}
</div>
  </div>
</body>

</html>

Here is the working Linkfor your code

这是您的代码的工作链接

Hope it helps you to understand :)

希望能帮助你理解:)