0

Im try to create a simple page and I have this problem when I I try to show my json file in my component.html

This is the json file.

{
  "example": {
    "cat": [
      {
        "title": "title 1",
        "href": "#cat0"
      },
      {
        "title": "title 1",
        "href": "#cat1"
      },
      {
        "title": "title 1",
        "href": "#cat1"
      },
      {
        "title": "title 1",
        "href": "#cat1"
      },
      {
        "title": "title 1",
        "href": "#cat1"
      }
    ]
  }
}

This is the Interface.

export interface menuExample {
    example?:{
        cat?:{
            title: string,
            href: string
        }
    }
}

This is the Service.

export class serviceMenu {

    info: menuExample= {};
    cargada = false;
    team: any[] = [];

    constructor( private http: HttpClient) { 

      this.loadMenu();

    }


    private loadMenu() {

      this.http.get('assets/json/file.json')
      .subscribe( (resp: any[]) => {

        this.cargada = true;
        this.team = resp;

      });  
    }
  }

And this is the html file

<li *ngFor="let menudetails of serviceMenu.info.example?.cat" class="side-nav__item">
 <a href="{{menudetails.href}}" class="side-nav__link">
    <span>{{menudetails.title}}</span>
 </a>
</li>

Then I import my file and I did all but I don't understand the mistake :S

ERROR Error: StaticInjectorError(AppModule)[HeaderComponent -> serviceMenu ]: StaticInjectorError(Platform: core)[HeaderComponent -> serviceMenu ]: NullInjectorError: No provider for serviceMenu !

Only I try to show my json with an NgFor and show the title and href, nothing else. Someone could help me or provide a better and easy solution? Thanks a lot!

Estefa

Abhishek
  • 1,742
  • 2
  • 14
  • 25
Estefa77
  • 9
  • 1

3 Answers3

0

You need add @Injectable() in serviceMenu class and add providers: [ serviceMenu ] in AppModule class

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
0

you need to add your service in your appmodule or in your component

import {serviceMenu} from '<your service path>'
@Component({
    selector: 'componentName',
    templateUrl: './componentName.html',
    providers: [serviceMenu]
})

or you can also add your service in providers section in appmodule.ts

import {serviceMenu} from '<your service path>'
providers: [serviceMenu]
Yash Rami
  • 2,276
  • 1
  • 10
  • 16
0

Error look like you have missed providers in AppModule. you need to add service in AppModule

@NgModule({
declarations: [];
imports: [];
providers: [ serviceMenu ]  // like this
})
Abhishek
  • 1,742
  • 2
  • 14
  • 25