0

I'm starting to learn angular. I'm trying to occupy the datatables library that I use in other projects with other languages. By reading the documentation here: https://l-lin.github.io/angular-datatables/#/basic/with-ajax. But I have not managed to access the information in the static json file.

import { Component,  OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  dtOptions: DataTables.Settings = {};

  ngOnInit(): void {
    this.dtOptions = {
      ajax: 'data/data.json',
      columns: [{
        title: 'ID',
        data: 'id'
      }, {
        title: 'First name',
        data: 'firstName'
      }, {
        title: 'Last name',
        data: 'lastName'
      }]
    };
  }
}

the problem is in the ajax call (I think), but I tried:

'./data/data.json'
'..data/data.json'
'data/data.json'

html

<table datatable [dtOptions]="dtOptions" class="row-border hover"></table>

Console:

enter image description here

And many other things but I can not get to the result, it's the first time I spend so much time solving something with this library :(

2
  • Are you using Angular-CLI version 6.0.1? Commented Jun 30, 2018 at 8:39
  • 6.0.3 sorry for don't write the version before Commented Jun 30, 2018 at 10:29

1 Answer 1

1

In this situation, when using the angular-cli, that file location will not be found at run-time (using the ng serve command).

You have to put your json file in the assets folder, and your path should be like this:

assets/data/data.json

Please check the screenshot below:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Been looking at this issue for a while, thanks for this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.