angularjs + jquery:如何在Foreach循环中访问数组中的数组元素?

时间:2013-10-12 15:18:14

标签: jquery arrays angularjs

我有一个对象阵列'假期'

$scope.Vacations = [{Id:'1' ,VacationType = {TypeId='1' ,TypeName = 'test'}}];

它包含其他对象数组'VacationType',只有一组

我想提取TypeId

我做过这样的事情:

$scope.selectedVacationType = [];
$scope.TypeTd;

$scope.selectedVacationType=Vacations.VacationType;
$scope.TypeTd;= $scope.selectedVacationType[0].Id;

但这不起作用

可以做些什么?

1 个答案:

答案 0 :(得分:1)

使用$scope.selectedVacationType=Vacations[0].VacationType;

Vacations代表项目列表[...]

顺便说一句,您的模型有误,删除=并替换为:

 $scope.Vacations = [{
        Id: '1',
        VacationType : {
           TypeId : '1',
           TypeName : 'test'
        }
    }];

<强> HTML

selectedVacationType: <pre>{{selectedVacationType|json}}</pre>
TypeTd:               <pre>{{TypeTd|json}}</pre>
TypeName:             <pre>{{TypeName|json}}</pre>

<强>控制器

    $scope.Vacations = [{
    Id: '1',
    VacationType : {
    TypeId : '1',
        TypeName : 'test'
    }
}];

$scope.selectedVacationType = $scope.Vacations[0].VacationType;
$scope.TypeTd = $scope.selectedVacationType.TypeId;
$scope.TypeName = $scope.selectedVacationType.TypeName;

演示 Fiddle