如何在AngularJS指令中将对象转换为数组

时间:2014-07-22 09:52:06

标签: arrays json angularjs angularjs-directive

以下是获取分页指令的一些代码

scope.pagingOptions = {
               pageSizes: [5, 10, 15,],
               pageSize: 5,
               currentPage: 1
                     };
              scope.filterOptions = {
                 filterText: "",
                 useExternalFilter: true
                    }; 
                 scope.totalServerItems = 0;
         scope.setPagingData = function(data, page, pageSize){  

                $rootScope.griddata =scope.griddata;

                var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
                   scope.griddata = pagedData;

                  scope.totalServerItems = data.length;
                        if (!scope.$$phase) {
                             scope.$apply();
                                }
                                };
                scope.getPagedDataAsync = function (pageSize, page, searchText) {

                setTimeout(function () {
                 var data;
                   if (searchText) {
                      var ft = searchText.toLowerCase();
                                 } else {

                            scope.setPagingData($rootScope.myData,page,pageSize);
                                     }
                                   }, 500);
                                };

                                        scope.getPagedDataAsync(scope.pagingOptions.pageSize, scope.pagingOptions.currentPage);

                        scope.$watch('pagingOptions', function (newVal, oldVal) {

                      if (newVal !== oldVal ) {
                                                          scope.getPagedDataAsync(scope.pagingOptions.pageSize, scope.pagingOptions.currentPage, scope.filterOptions.filterText);
                                        }
                                    }, true);
                    scope.$watch('filterOptions', function (newVal, oldVal) {

                    if (newVal !== oldVal) {
                                                          scope.getPagedDataAsync(scope.pagingOptions.pageSize, scope.pagingOptions.currentPage, scope.filterOptions.filterText);
                                        }
                                        }, true);

                                  },500);                                         

控制器中的代码

$rootScope.myData = bankService
                    .getWorkItemsByBank($rootScope.selectedBankName);

在控制器中,我正在命中数据库,并在$ rootScope.myData变量中得到结果,在我的指令中,我使用这个相同的变量传递给方法" setPagingData"但我在下面一行的函数中未定义

data.slice((page - 1) * pageSize, page * pageSize);

存储在$ rootScope.myData中的结果如下所示

 {"$$v":[{"Number":"35973","id":"09510uiy8240E","ItemType":"Application", "amount":"USD","aWorker":"WorkerAMS1","aWorkGroupType":"Unassigned","receiptDate":"2008-12-04","receiptTime":"","curTrkStatuses":"REL, CMP"},{"Number":"35973","id":"09510uiy8240E","ItemType":"Application", "amount":"USD","aWorker":"WorkerAMS1","aWorkGroupType":"Unassigned","receiptDate":"2008-12-04","receiptTime":"","curTrkStatuses":"REL, CMP"}]}

我认为上面的值是一个对象但是data.slice方法适用于数组,并且还尝试将对象转换为也没有工作的数组。所以请建议我如何解决这个问题。

0 个答案:

没有答案