AngularJS pagination and Cannot read property 'slice' of undefined

五迷三道 提交于 2019-12-24 11:58:03

问题


Iv'e got an problem. Please help me what is wrong.

There is my code :

(function () {

    var app = angular.module('store', ['ui.bootstrap']);

    app.controller('StoreController', function ($scope, $http) {
        $scope.pageSize = 20;
        $scope.currentPage = 1;
        $http.get("../scripts/products.php").success(function (data) {
            $scope.products = data;
        });

    }).filter('startFrom', function () {
        return function (data, start) {
            return data.slice(start);
        };
    });
})();

And HTML :

<div class="col-md-2 product " ng-hide="product.soldOut" ng-repeat="product in products| orderBy:sortorder | filter: searchBox | startFrom:(currentPage - 1) * pageSize | limitTo: pageSize">
    <img src="../products/{{product.image}}">
    <h4>{{product.nazwa}}
        <br>
        <br>
    </h4>
    <strong>{{product.cena| currency:"PLN "}}</strong>

    <div class="colored">{{product.usagee}}</div>
    <div class="buttons">
        <a href="/product/{{product.id}}" class="btn btn-default btn-sm" style="">Szczegóły</a>
        <a href="/product/{{product.id}}" class="btn btn-success btn-sm" ng-show="product.dostepny">Kup</a>
    </div>
</div>
<div class="col-md-12">
    <uib-pagination class="pagination-sm" previous-text="Poprzednia" next-text="Następna" total-items='products.length' ng-model='currentPage' items-per-page='pageSize'></uib-pagination>
</div>

I was searching in google and stack but i can't find any good solution.


回答1:


You have to check that $scope.products is defined it seems like it's undefined before the server returns its data.

You can set a check in the filter for data to be defined or initalize products in the controller to be an empty array.



来源:https://stackoverflow.com/questions/33584483/angularjs-pagination-and-cannot-read-property-slice-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!