【问题标题】:Angular 2 Load data through server API : data.slice errorAngular 2 通过服务器 API 加载数据:data.slice 错误
【发布时间】:2017-12-25 18:47:41
【问题描述】:

我正在尝试使用 Angular2 ng 智能表插件将数据从我的 API 加载到自定义组件。

根据他们的文档 (https://github.com/akveo/ng2-smart-table/blob/master/src/app/pages/examples/server/basic-example-load.component.ts)

我的组件如下:

import { LocalDataSource } from 'ng2-smart-table';
import { ProductService } from '../../../services/product.service';

export class CategoryItemsComponent implements OnInit {

...
 source: LocalDataSource;

 constructor(private productService: ProductService,
             private flashMessage: FlashMessagesService,
             private router: Router,
             http: Http) {
       this.source = new LocalDataSource();

      this.productService.getProductsOncategory(this.categoryid).subscribe((data) => {
      this.source.load(data);
   });

}

产品服务 .ts

getProductsOncategory(category_id) {

let catUrl = "http://localhost:5000/products/getProductsOncategory"
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let catIdObj = JSON.stringify({ category_id: category_id })
return this.http.post(catUrl, catIdObj, { headers: headers })
 .map((response: Response) => response.json())
  .do(data => console.log(JSON.stringify(data)))
  .catch(this.handleError);
}

服务功能中使用的上述API在我的邮递员中完美运行。

现在我需要将该 API 中的 dame 数据加载到我的自定义组件中。

我收到此错误:

错误 TypeError: this.data.slice 不是函数

在 LocalDataSource.webpackJsonp.../../../../ng2-smart-table/lib/data-source/local/local.data-source.js.LocalDataSource.getElements (http://localhost:4200/1.chunk.js:22280:30) 在 LocalDataSource.webpackJsonp.../../../../ng2-smart-table/lib/data-source/data-source.js.DataSource.emitOnChanged (http://localhost:4200/1.chunk.js:22185:14) 在 LocalDataSource.webpackJsonp.../../../../ng2-smart-table/lib/data-source/data-source.js.DataSource.load (http://localhost:4200/1.chunk.js:22105:14) 在 LocalDataSource.webpackJsonp.../../../../ng2-smart-table/lib/data-source/local/local.data-source.js.LocalDataSource.load (http://localhost:4200/1.chunk.js:22243:38)

【问题讨论】:

    标签: angularjs node.js mean-stack ng2-smart-table


    【解决方案1】:

    好的,我用like得到了它:

    source: LocalDataSource;
    
    constructor(private productService: ProductService,
                private flashMessage: FlashMessagesService,
                private router: Router,
                http: Http) 
    {
       this.source = new LocalDataSource();
    }
    
    onChange(categoryid) {
    this.productService.getProductsOncategory(categoryid).subscribe(data => {
      if (data.success) {
        this.source.load(data.products);
        console.log('Products obtained');
      } else {
        console.log('Not obtained!');
      }
    });
    
    }
    

    【讨论】:

      【解决方案2】:

      遇到了同样的问题。当我检查所有匹配列并在表数据中添加缺失的内容时,它解决了。例如我声明了一个变量

      Settings = { 
      ....
      columns: {
          id: {
          title: 'id',
          show: false,
          type: 'string',
        },
        name: {
          title: 'name',
          type: 'string',
        },
        //compare columns in json response in same variable declaration
        //for your data table [source]
       }
      }
      

      在第二种情况下,您的表尝试在完全加载数据之前从 dataTableSource 获取数据,以避免这种情况使用setTimeout(); 方法并设置更多时间。 例如:

      getChildData(): Promise<any> {
          return new Promise((resolve, reject) => {
              setTimeout(() => {
                  resolve(this.childsList);
              }, 4000);//<= increase this value
          });
      }
      

      对不起我的英语))

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-07
        • 2020-06-07
        • 2017-09-28
        • 1970-01-01
        • 1970-01-01
        • 2017-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多