Вы должны использовать angular.isDefined
, чтобы проверить, существует ли свойство items
в каждом объекте data
, а затем пройти через него, чтобы получить значение text
в массиве items
:
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
var data= [
{ text: "Item 1" },
{ text: "Item 2", items: [
{ text: "SubItem 2.1" },
{ text: "SubItem 2.2" }
] },
{ text: "Item 3" }
];
angular.forEach(data, function (value, key)
{
console.log(value.text);
if(angular.isDefined(value.items)){
var itemsArray = value.items;
angular.forEach(itemsArray, function (item, key){
console.log(item.text);
});
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl"></div>