Angular Datatable无法从变量中分配数据

问题描述 投票:0回答:1

我正在尝试将数据分配给dtOptions - 但不起作用。这样做的正确方法是什么?

这是我的功能:

 getDatas(){
        this.datas = this.http.get('data/data.json').subscribe(values =>  values);//values available  here
    }

    ngOnInit() {

        this.categoryListCopy = this.categoryList;
        this.getDatas()

        const that = this;
        this.dtOptions = {

            columns:[{
                data: 'id'
            }, {
                data: 'firstName'
            }, {
                data: 'lastName'
            }],
            pagingType: 'full_numbers',
            pageLength: 10,
            serverSide: false,
            processing: true,
            ajax: this.datas
            };



        }
angular7 angular-datatables
1个回答
1
投票

您只需要使用Observable,您的代码将如下所示:

getDatas(): Observable{
        return this.http.get('https://jsonplaceholder.typicode.com/todos/1');
    }

  ngOnInit() {
        this.getDatas().subscribe(values =>  {
           console.log(values);
           // You can assign dtOptions here, you will have your data in values.
        });      
  }

你可以找到工作插头here

© www.soinside.com 2019 - 2024. All rights reserved.