8

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"

. 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.

3
  • 3
    you could use ng-if="$first" inside ng-repeat Commented Sep 9, 2015 at 19:17
  • check here : docs.angularjs.org/api/ng/directive/ngRepeat... check Oliver Smith's answer. He got it. Commented Sep 9, 2015 at 19:25
  • I have updated my question. Please give me a sollution Commented Sep 9, 2015 at 19:49

3 Answers 3

20

Well just doing the following will work:

<td>
    {{p[0]}}
</td>
Sign up to request clarification or add additional context in comments.

Comments

8

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

Try this

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

1 Comment

Should it not be "x in p |limitTo:1" ?
1

For your condition, you can use ng-if inside ng-repeat as Pankaj said in the comment.

  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 Link for your code

Hope it helps you to understand :)

3 Comments

Please see my code again. I have updated the question.
and now if I want to take the first value of images "236.jpg", what I have to do ?
I am taking the array from PHP file. So how I will manage to make the array like you did now ? My PHP code is json_encode JSON

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.