最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Display an Array inside Object using ng-repeat -AngularJs - Stack Overflow

programmeradmin3浏览0评论

Below is my JSON object which I would like to display the name in both the parent and child array.

   $scope.result= [
        {
            "id": 1,
            "name": "1002",

            "parentArray": [
                {
                    "id": 28,
                    "name": "PRODP1",
                    "shortCode": "PRODP1"               
                }
            ]        

        }

I want to display Name:1002 Parent_Name:PRODP1

I tried {{item.name}} which will only display 1002.But I need to display the name of parentArray as well.

Below is my JSON object which I would like to display the name in both the parent and child array.

   $scope.result= [
        {
            "id": 1,
            "name": "1002",

            "parentArray": [
                {
                    "id": 28,
                    "name": "PRODP1",
                    "shortCode": "PRODP1"               
                }
            ]        

        }

I want to display Name:1002 Parent_Name:PRODP1

I tried {{item.name}} which will only display 1002.But I need to display the name of parentArray as well.

Share Improve this question asked Apr 22, 2015 at 7:48 forgottoflyforgottofly 2,72912 gold badges54 silver badges97 bronze badges 3
  • {{ item.parentArray[0].name }} ? – fracz Commented Apr 22, 2015 at 7:49
  • @fracz When there are more than one parentArray? – forgottofly Commented Apr 22, 2015 at 7:51
  • how do you want to handle if more than one parent arrays.. update your question with sample example of how should that case be displayed – harishr Commented Apr 22, 2015 at 7:57
Add a ment  | 

2 Answers 2

Reset to default 5

Since the parentArray is also an array your going to need a nested ng-repeat.

If this is a large page then this may cause a performance issue.

<div ng-repeat="item in result">
     {{item.name}}
     <div ng-repeat="innerItem in item.parentArray">
           {{innerItem.name}}
     </div>
</div>

parentArray is an...array, so you need to access it using an index:

<div ng-repeat="item in result">
    Name: {{ item.name }} Parent_Name: {{ item.parentArray.length ? item.parentArray[0].name : '' }} 
</div>

That's under the assumption that there is one object in parentArray. You might need to iterate it, or you might need to check to see if it exists depending on your requirements.

发布评论

评论列表(0)

  1. 暂无评论