1

I have the following json example:

posts: [
    {
       title: Post Title,
     - cat: [
            - {
                name: "Category Name",
              }
            ],

I am able to get the title in Angular using {{post.title}}

However when I try to get {{post.cat.name}} its not working and its returning "0"

2
  • try this {{post.cat[0].name}} Commented May 20, 2016 at 8:37
  • Its not working still getting 0 Commented May 22, 2016 at 8:15

3 Answers 3

1

That's because cat is an array. You should handle it like that.

{{posts.cat[0].name}} if you want the first element in that array.

Usually for arrays you'll use ng-repeat though:

<span ng-repeat="cat in posts.cat">{{cat.name}}</span>
Sign up to request clarification or add additional context in comments.

Comments

1

try this.

var app = angular.module("app",[]);

app.controller("MyCtrl" , function($scope){
  
  $scope.posts=[
    {
       title: "Post Title",
       cat: [
           {
                name: "Category Name",
           }
          ]
    },
     {
       title: "Post Title2",
       cat: [
           {
                name: "Category Name2",
           }
          ]
    }
  
  ];
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
   
  <table>
     <tr ng-repeat="p in posts" >
       
         <td >{{p.title}}</td>
         <td >{{p.cat[0].name}}</td>
     </tr>
 </table>
  
</div>

Comments

0

cat is an array, so to get the name of the first object in that array, you would have to use:

{{post.cat[0].name}}

Comments

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.