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

时间:2015-09-09 19:09:35

标签: javascript arrays angularjs

这是数组

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

我想访问

  

“236.jpg”

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

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

请帮我找出溶剂。

这里有更多完整代码

{"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>

现在请帮我完整代码。谢谢。

3 个答案:

答案 0 :(得分:14)

只需执行以下操作即可:

 AlertDialog.Builder alertadd = new AlertDialog.Builder(
                SummaryActivity.this);
    LayoutInflater factory = LayoutInflater.from(SummaryActivity.this);
    final View view = factory.inflate(R.layout.social_network_selection, null);
    alertadd.setView(view);
    googleButton2 = (ImageButton) view.findViewById(R.id.googleButton);
    twitterButton2 = (ImageButton) view.findViewById(R.id.twitterButton);

    googleButton2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

               Toast.makeText(MyAndroidAppActivity.this,
                "googleButton2 is clicked!", Toast.LENGTH_SHORT).show();

            }

        });

答案 1 :(得分:7)

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

试试这个

/flaskapp

答案 2 :(得分:1)

根据您的情况,您可以在ng-if内使用ng-repeat,因为Pankaj在评论中说。

  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>

以下是代码的工作Link

希望它能帮助您理解:)