Search code examples
nativescriptangular2-nativescriptnativescript-angular

Can't see values in console.log() in nativescript


I have an js file which includes an array! The js file is like this one

export const appointments = [
{
  "taskID": 9018929,
  "resource_ID": 1,
  "title": "KNDL Thatcham",
  "description": "<Non-Selected>\r\n10:00 - 18:00",
  "startTime": "2018-12-28T00:00:00",
  "endTime": "2018-12-28T23:59:00",
  "statusColour": "#DFDFDF",
  "clientColour": "#FFFDD7"
}]

I have imported this js files in service.ts. And this appointments.js does not show in the console.log.

import { Injectable } from '@angular/core';
        import { appointments } from '../appointments';
        import { Color } from "tns-core-modules/color";
        // >> angular-calendar-require
        import { CalendarEvent } from 'nativescript-ui-calendar';


        @Injectable({
        providedIn: 'root'
        })


    export class CalendarService {

      constructor() { 
      //var a="test test etsdhsakdsakd dsddssdsd";
      console.log(appointments);
    } 

This is the component where I am importing the service.ts

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

import { CalendarService } from "./service/calendar.service";
import { RadCalendar, CalendarEvent, CalendarSelectionEventData } from "nativescript-ui-calendar";
@Component({
  selector: 'ns-calendar',
  templateUrl: './calendar.component.html',
  styleUrls: ['./calendar.component.css'],
  moduleId: module.id,
  providers: [CalendarService]
})
export class CalendarComponent implements OnInit {
}

Solution

  • You need to add a private calendarService parameter of type CalendarService to the constructor.

    When Angular creates a CalendarComponent , the Dependency Injection system sets the calendarService parameter to the singleton instance of CalendarService .

    export class CalendarComponent implements OnInit { constructor(calendarService : CalendarService ) {}

    }