我收到TypeError:从工厂ng-Table AngularJS加载数据时,无法调用
发布时间:2020-12-17 07:11:45 所属栏目:安全 来源:网络整理
导读:我是AngularJS的新手,在使用来自服务的ng-Table时,他一直在显示数据.我收到了一个错误 TypeError:无法调用未定义的方法’slice’ ????at Object.$scope.tableParams.ngTableParams.getData(index.html:122:32) 当我对json数据值进行硬编码时,它的工作正常
我是AngularJS的新手,在使用来自服务的ng-Table时,他一直在显示数据.我收到了一个错误
TypeError:无法调用未定义的方法’slice’ 当我对json数据值进行硬编码时,它的工作正常.我认为来自工厂的数据不仅仅包含json数据结果,因此切片存在问题. 我的控制器看起来像这样: myApp.controller('usersCtrl',function usersCtrl($scope,userData,$filter,ngTableParams,$log){ userData.getUsers(function(users){ $scope.users = users; }) var users = $scope.users; $scope.$watch("filter.$",function () { $scope.tableParams.reload(); }); $scope.tableParams = new ngTableParams({ page: 1,// show first page count: 10,// count per page sorting: { name: 'asc' // initial sorting } },{ getData: function($defer,params) { var filteredData = $filter('filter')(users,$scope.filter); var orderedData = params.sorting() ? $filter('orderBy')(filteredData,params.orderBy()) : filteredData; $defer.resolve(orderedData.slice((params.page() - 1) * params.count(),params.page() * params.count())); },$scope: $scope }); }); 那我的工厂服务是这样的: myApp.factory('userData',function($http,$log){ return { getUsers: function(successcb){ $http({method: 'GET',url: 'api/users'}). success(function(data,status,headers,config){ successcb(data); $log.warn(data,config); }). error(function(data,config){ $log.warn(data,config); }); } } }); 我的HTML是这样的: <div class="row"> <div> <p>Filter: <input class="form-control" type="text" ng-model="filter.$" /></p> <table ng-table="tableParams" class="table"> <tr ng-repeat="user in $data"> <td data-title="'Name'" sortable="name"> {{user.name}} </td> <td data-title="'Age'" sortable="'age'"> {{user.age}} </td> </tr> </table> </div> </div> 解决方法
在ajax完成后,您需要在表getData中解析$defer.现在,如果您要将filteredData记录到控制台,它将是未定义的,因此无法进行切片.
尝试将userData.getUsers移动到: getData: function ($defer,params) { /* make ajax call */ userData.getUsers(function(users) { /* now we have data to work with*/ $scope.users = users; var filteredData = $filter('filter')(users,$scope.filter); var orderedData = params.sorting() ? $filter('orderBy')(filteredData,params.orderBy()) : filteredData; /* and can resolve table promise */ $defer.resolve(orderedData.slice((params.page() - 1) * params.count(),params.page() * params.count())); }) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 通过jaxws-ri于webservice与一次亲密接触
- Scala Future map由于缺少ClassTag而无法编译
- 7.1 安装软件包的三种方法 7.2 rpm包介绍 7.3 rpm工具用法
- Bash脚本:如何等待进程的某些输出,然后继续?
- AngularJS指令:模糊之后不会触发ng-click
- Scala – Combinator Parsing,替代品的顺序似乎很重要
- 【Angular2】 如何创建一个Angular2项目
- 如何结合vim wiki插件的两个功能?
- angularjs – 错误:[$injector:nomod]模块’ui.bootstrap
- 数组 – 在Scala中的apply()函数给出编译错误?