JSON - 替换外部JSON文件中的$ scope对象

时间:2015-03-27 21:46:22

标签: javascript json angularjs

我正在尝试从我的应用程序中的外部JSON文件中获取数据。我的大多数数据都加载在DataTables表中,但我的JSON数据包括$ scope,我认为这不是有效的,因为它是外部的。 status和errorCode的数据已经在同一个JSON文件中。我可以用$scope.status[i]$scope.errorCode[i]代替什么才能加载其他数据?

我的JSON代码的一部分:

"status" : [
{"id": 1, "name": "Active"},
{"id": 2, "name": "Inactive"},
{"id": 3, "name": "Unknown"}
],

"errorCode" : [
    {"id": 1, "name": "Code01"},
    {"id": 2, "name": "Code02"},
    {"id": 3, "name": "Code03"},
    {"id": 4, "name": "Code04"}
    ],

"apps" : [
    {"appName": "App01",
        "dateCreated": "01/01/2015",
        "dateUpdated": "01/04/2015",
        "payload": "Payload01",
        "status": "$scope.status[0]",
        "errorCode": "$scope.errorCode[0]",
        "errorDesc": "Desc01",
        "recordCount": 1,
        "recordFail": 1},
    {"appName": "App01",
        "dateCreated": "01/02/2015",
        "dateUpdated": "01/05/2015",
        "payload": "Payload02",
        "status": "$scope.status[0]",
        "errorCode": "$scope.errorCode[1]",
        "errorDesc": "Desc02",
        "recordCount": 1,
        "recordFail": 2},
    {"appName": "App03",
        "dateCreated": "01/03/2015",
        "dateUpdated": "01/06/2015",
        "payload": "Payload03",
        "status": "$scope.status[1]",
        "errorCode": "$scope.errorCode[2]",
        "errorDesc": "Desc03",
        "recordCount": 2,
        "recordFail": 1}

调用JSON数据的HTML代码:

<table id="myTable" class="table display">
    <thead>
        <tr>
            <th ng-repeat="(i, th) in head" value="{{th.id}}" class="{{th.class}}"><span>{{th.name}}</span></th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="app in apps | filter:search" ng-click="clickRow(app)" ng-class="{selected: app === selectedRow}">
            <td>{{app.appName}}</td>
            <td>{{app.dateCreated}}</td>
            <td>{{app.dateUpdated}}</td>
            <td>{{app.payload}}</td>
            <td>{{app.status.name}}</td>
            <td>{{app.errorCode.name}}</td>
            <td>{{app.errorDesc}}</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <th ng-repeat="(i, th) in head" value="{{th.id}}"><span>{{th.name}}</span></th>
        </tr>
    </tfoot>
</table>

<table class="table display">
    <thead>
        <tr>
            <th ng-repeat="(i, th) in details" value="{{th.id}}"
                class="{{th.class}}"><span>{{th.name}}</span></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>{{selectedRow.appName}}</td>
            <td>{{selectedRow.errorDesc}}</td>
            <td>{{selectedRow.recordCount}}</td>
            <td>{{selectedRow.recordFail}}</td>
            </tr>
    </tbody>
</table>

感谢任何输入!

1 个答案:

答案 0 :(得分:0)

我更改了我的JSON数据以调用statuserrorCode的索引而不是调用范围:

"status": 0,
"errorCode": 0,

我还更改了HTML以反映更改,因此我的数据会显示在表格中:

<td>{{status[app.status].name}}</td>
<td>{{errorCode[app.errorCode].name}}</td>