数组中的对象未显示在ng-repeat中 - AngularJS

时间:2016-01-19 09:28:21

标签: javascript arrays angularjs

我对AngularJS很陌生,我在视图中遇到一些显示数组对象的问题。

我有以下javascript代码:

// list initialization:
vm.latestInvoices = { invoices: []};
....

// adding function to viewmodel:
vm.getLatestInvoices = getLatestInvoices;
...

    function getLatestInvoices() {
        console.log("Test 1234");
        var servicename = "GetLatestInvoicesRequest";

        var params = {};

        httpCall(servicename, params).then(function (data) {
            vm.latestInvoices = data;
            console.log(data);
            return vm.latestInvoices;
        })["catch"](function (data) {
            console.log("getLatestInvoices error");
        });
    }

我的HTML视图:

<div class="col-lg-12">
        <p id="overview-headline">Invoices Overview</p>
        <div class="scrollCustomers" ng-init="vm.getLatestInvoices()">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Client</th>
                        <th>Project</th>
                        <th>ID</th>
                        <th>Inv. Date</th>
                        <th>Start Date</th>
                        <th>End Date</th>
                        <th>DKK ex VAT</th>
                        <th>CIG</th>
                        <th>Attention</th>
                        <th>Cust. Manager</th>
                        <th>Regarding</th>
                        <th>Due Date</th>
                        <th>Finalized</th>
                        <th>Paid</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="invoice in invoices">
                        <td>{{invoice.CompanyName}}</td>
                        <td>Project Name</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td>Default</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td>N/A</td>
                        <td>N/A</td>
                        <td>N/A</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

在Google Dev控制台中,数组及其对象可以正常返回。视图不会返回默认值,项目名称,默认值和N / A,所以我的猜测是,由于某种原因,ng-repeat中的发票永远不会被调用?

提前致谢!

1 个答案:

答案 0 :(得分:1)

将结果附加到$scope

httpCall(servicename, params).then(function (data) {
        vm.latestInvoices = data;
        console.log(data);
        $scope.invoices = data;
    })["catch"](function (data) {
        console.log("getLatestInvoices error");
    });
}