简体   繁体   English

ngTable将不会重新加载数据

[英]ngTable Will Not Reload Data

I'm an experienced C# and MVC programmer, but just starting out with AngularJS. 我是一位经验丰富的C#和MVC程序员,但只是从AngularJS开始。 I've been using ngTable (trying to anyway) with limited success, but one issue I cannot resolve - no matter what I do, I cannot get a particular table to refresh when the data has changed. 我一直在使用ngTable(无论如何尝试),但效果有限,但我无法解决一个问题-无论如何,当数据更改时,我都无法刷新特定的表。

I'm getting data from a Web API via a factory - it's a list of suppliers associated with a particular Product. 我正在通过工厂从Web API获取数据-这是与特定产品相关的供应商的列表。 The first time the screen is loaded, the data is brought back and the table displays fine - any subsequent call, the data is returned, but the table is not updating. 第一次加载屏幕时,数据将恢复到原来的状态,并且该表显示良好-任何后续调用都将返回数据,但该表未更新。 Other areas of the page are updating as expected. 页面的其他区域正在按预期更新。

I've done some console logging, and can see that the data is coming back. 我已经完成了一些控制台日志记录,可以看到数据又回来了。 I've tried the tableParams.reload() function, and setting the tableParams.total() but nothing seems to trigger the table to refresh. 我已经尝试过tableParams.reload()函数,并设置了tableParams.total()但是似乎没有什么触发表刷新。

This is my function in the Controller: 这是我在Controller中的功能:

function getStockSupplier() {
        console.log("getStockSupplier()");
        $scope.stockSupplierTableParams = {};

        stockSupplier.getAll({ url: "localhost:52457", sku: $scope.model.sku })
            .$promise.then(function (response) {
                $scope.stockSupplier = response;

                $scope.stockSupplierTableParams = new NgTableParams({
                }, {
                    dataset: response
                });

                console.log("Got result");
                console.log("Length: " + $scope.stockSupplierTableParams.settings().dataset.length);

                $scope.stockSupplierTableParams.reload();

            }, function (response) {
                alert('no stock item');
                $scope.stockSupplier = null;
            });
    }

And this is my HTML code: 这是我的HTML代码:

<div ng-controller="stockController">
        <div>
            <table ng-table="stockSupplierTableParams" class="table table-condensed table-bordered table-striped">
                <tr ng-repeat="issue in $data">
                    <td data-title="'Supplier1'">
                        {{issue.SupplierName}}
                    </td>
                    <td data-title="'On Order'">
                        {{issue.OnOrder}}
                    </td>
                </tr>

            </table>
        </div>
    </div>

I'm at a complete loss - it may be something fundamental I'm missing, but it's driving me crazy, so any help great appreciated. 我完全不知所措-这可能是我所缺少的基本知识,但是它使我发疯,所以任何帮助都值得赞赏。

EDIT: Here's the code for the Web API Service call, in case that has any relevance. 编辑:如果有任何相关性,这是Web API服务调用的代码。 Also, I should point out that the HTML above is used in a Directive, if that makes any difference. 另外,我要指出的是,上述HTML用于指令中,如果有什么不同的话。

var myService = angular.module('myService', ['ngResource']);
myService.factory('stockSupplier', [
    '$resource',
    function ($resource) {
        return $resource('http://:url/api/StockInfo?Sku=:sku&StockSupplier=true',
            {
                url: '@url',
                sku: '@sku'
            },
            {
                getAll: {
                    method: 'GET',
                    isArray: true
                },
            });
    }
]);

I have a simple solution, you can re-render table when data loaded: 我有一个简单的解决方案,可以在加载数据时重新呈现表:

HTML HTML

 <div ng-controller="stockController">
    <div data-ng-if="tableIsReady">
        <table ng-table="stockSupplierTableParams" class="table table-condensed table-bordered table-striped">
            <tr ng-repeat="issue in $data">
                <td data-title="'Supplier1'">
                    {{issue.SupplierName}}
                </td>
                <td data-title="'On Order'">
                    {{issue.OnOrder}}
                </td>
            </tr>

        </table>
    </div>
    <div data-ng-if="!tableIsReady">
       // you can put loader here
   </div>
</div>

JS JS

 function getStockSupplier() {
    console.log("getStockSupplier()");
    $scope.stockSupplierTableParams = {};

    $scope.tableIsReady = false; // here we hide table, and show loader

    stockSupplier.getAll({ url: "localhost:52457", sku: $scope.model.sku })
        .$promise.then(function (response) {
            $scope.stockSupplier = response;

            $scope.stockSupplierTableParams = new NgTableParams({
            }, {
                dataset: response
            });

            console.log("Got result");
            console.log("Length: " + $scope.stockSupplierTableParams.settings().dataset.length);

            $scope.stockSupplierTableParams.reload();

            $scope.tableIsReady = true; // here we show table again

            $scope.$apply() // start digest

        }, function (response) {
            alert('no stock item');
            $scope.stockSupplier = null;
        });
}

Otherwise, you code is fine, you just may be forget put $scope.$apply() after $scope.stockSupplierTableParams.reload(); 否则,您的代码会很好,您可能会忘记将$scope.stockSupplierTableParams.reload();放在$scope.$apply()之后$scope.stockSupplierTableParams.reload();

I got it working! 我知道了! Though I'm not 100% sure how, so if any experienced Angular developers could explain it, I would be grateful. 尽管我不是100%不确定如何做,但是如果有经验的Angular开发人员可以解释这一点,我将不胜感激。 There are two parts to it. 它有两个部分。

Basically - instead of setting the dataset to the response object of the API call, I set it to the variable $scope.stockSuppler . 基本上,不是将数据集设置为API调用的响应对象,而是将其设置为变量$scope.stockSuppler Then, I explicitly empty this variable before the update - code below: 然后,我在更新之前显式清空此变量-下面的代码:

function getStockSupplier() {
        console.log("getStockSupplier()");
        $scope.tableIsReady = false;
        $scope.stockSupplierTableParams = {};
        $scope.stockSupplier = [];

        stockSupplier.getAll({ url: "localhost:52457", sku: $scope.model.sku })
            .$promise.then(function (response) {
                $scope.stockSupplier = response;

                $scope.stockSupplierTableParams = new NgTableParams({
                }, {
                    dataset: $scope.stockSupplier
                });

                console.log("Got result");
                console.log("Length: " + $scope.stockSupplierTableParams.settings().dataset.length);
                $scope.stockSupplierTableParams.reload();
                $scope.tableIsReady = true;
            }, function (response) {
                alert('no stock item');
                $scope.stockSupplier = null;
            });
    }

Also, I removed the ngController="stockController" from the directive HTML code - the directive is called in a div which already defined the controller. 另外,我从指令HTML代码中删除了ngController="stockController" -该指令在已定义控制器的div中调用。 Having the controller defined twice appears to have been confusing it as well. 两次定义控制器似乎也使它感到困惑。 I should emphasis that only be making both of these changes, I got to to work. 我应该强调,只有同时进行这两项更改,我才能开始工作。 One or the other only, it still doesn't work correctly. 仅一个或另一个,它仍然无法正常工作。

The table is now updating as expected with the other parts on the page. 现在该表正在按页面上的其他部分进行更新。

I'm not sure why using the scope variable rather than the response from the API makes a difference, but it's done the trick. 我不确定为什么使用范围变量而不是来自API的响应会有所作为,但这已经成功了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM