Angular 数据表无法从变量分配数据

Angular Datatable not able to assign the data from variable

我正在尝试将数据分配给 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
            };



        }

您只需要使用 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