如何访问数组中存在的多个对象的字段

how to access a field from multiple object which is present in array

我有一个名为 $scope.mobData=[];

的空数组

使用 $http.get(..) 我得到 10 json 对象并成功推送到数组,每个对象有 7 个数据,但我只需要每个对象中的两个字段称为 brandCode 和在所有 10 个对象中可用的 brandName。但在我的 UI 部分,我无法显示我的品牌名称和品牌代码数据,这是我的 html 代码

<table >
            <thead>
              <th>BrandCode</th>
              <th>BrandName</th>
              <th>Action</th>
            </thead>

            <tbody>
            <tr ng-repeat="mobData in mobDatas">
              <td>{{mobData[index].brandCode}}</td>
              <td>{{mobData[index].brandName}}</td>
              <td>
                <button type="button" class="button button-positive" ng-click="editMobData(mobData)">Edit</button>
                <button type="button" class="button button-assertive" ng-click="deleteMobData(mobData)">Delete</button>
              </td>
            </tr>
            </tbody>
</table>

当我使用 {{mobData[o].brandCode}} 时,我可以查看数据,但如果不使用 mob[0],我将无法查看我的数据。我也试过了 {{mobData.brandCode}} 我无法查看我的数据

mobData 是 object 并且您通过使用索引器像 collection 一样使用它,您可能需要直接访问属性。此外,如果您的 collection 名称是 mobData,那么 ng-repeat 将出现在 mobData 而不是 mobDatas.

我做了现场演示,其中我有一个 object 数组,其中包含字段 brandCode 和 brandName。我在 ng-repeat 中的 $scope 中使用该数组来呈现 table 行,正如您在问题中所拥有的那样。

Live Demo

<tr ng-repeat="mobDataObj in mobData">
       <td>{{mobDataObj.brandCode}}</td>
       <td>{{mobDataObj.brandName}}</td>

请注意,您必须设置 angular 的初始状态。

app = angular.module('aaa', []);
app.controller('ccc', function($scope) {
$scope.mobData = [{brandCode: 'brand code 1', brandName: 'Brand Name 1'},
                {brandCode: 'brand code 2', brandName: 'Brand Name 2'},
                {brandCode: 'brand code 3', brandName: 'Brand Name 3'}];

});

这样做:

<td>{{mobData.brandCode}}</td>
<td>{{mobData.brandName}}</td>

您不需要 $index,因为您已经拥有 mobData,与 mobDatas[$index]

相同

你的绑定好像有问题,应该是这样的

{{mobData.brandCode}}
{{mobData.brandName}}