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
2 Answers
Reset to default 5Since 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.