Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular service interprets method as a property?

I have created an Angular service for my Angular component, LiveDashboardComponent, with a method to instantiate the component’s property.

Component

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

import { LiveDashboardService } from './live-dashboard.service';


@Component({
  selector: 'c8y-live-dashboard',
  templateUrl: 'live-dashboard.component.html',
  styleUrls: ['live-dashboard.component.less'],
})
export class LiveDashboardComponent {
   days: string[] | undefined;

  constructor(private liveDashboardService: LiveDashboardService) {}

  ngOnInit() {
    this.getDays();
  }

  private getDays(): void {
    this.days = this.liveDashboardService.getDays();
  }
  
}

Service

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { Injectable } from "@angular/core";

@Injectable({
  providedIn: "root",
})
export class LiveDashboardService {
  private days: string[] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  
  constructor() {}
  
  private getDays(): string[] {
    return this.days.slice();
  }
  
}

I am having a problem whereby the compiler underlines the getDays method name, meaning that it does not include the parenthesis (), in my component’s implementation of the method with the following message.

Property 'getDays' is private and only accessible within class 'LiveDashboardService'

However, as the code snippet demonstrates, I do not have a property called getDays in my Angular service. Instead, it is a method, and I do not know why the compiler cannot identify it as such.

>Solution :

The error message is quite explicit, you can’t call a private method from the outside : make it public

  public getDays(): string[] {
    return this.days.slice();
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading