0

I have a question identical to this guy here:

Angular 2 Setter and Getter

The solution que provided thought is not enough for my project to work so I'll explain my situation with some more details to see if you guys can help me!

I have a service dataBase.service.ts :

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

@Injectable()
export class dataBaseService{

    serviceData: string;

    get data():string{
        return this.serviceData;
    }

    set data(value:string){
        this.serviceData = value;
    }
}

this service is obviously added to app.module.ts as a provider..

on my component A i have:

import { Component, OnInit } from '@angular/core';
import {dataBaseService} from "../dataBase.service";




@Component({
  selector: 'app-tipo',
  templateUrl: './tipo.component.html',
  styleUrls: ['./tipo.component.scss'],
    providers: [dataBaseService]
})
export class A implements OnInit {


  constructor( public dataService:dataBaseService) {
      this.dataService.serviceData = 'hello';
  }



  ngOnInit() {
      console.log(this.dataService.serviceData);
  }

}

Until Here everything is fine. If I show on console:

console.log(this.dataService.serviceData); it returns me "hello" as expected

but on my next component when I print again the same data it shows undefinied:

import { Component, OnInit } from '@angular/core';
import {dataBaseService} from "../dataBase.service";

@Component({
  selector: 'app-modelo',
  templateUrl: './modelo.component.html',
  styleUrls: ['./modelo.component.scss'],
    providers: [dataBaseService]
})
export class ModeloComponent implements OnInit {



  ngOnInit() {
      console.log(this.dataService.serviceData);      
  }
    constructor(public dataService: dataBaseService) {

    }

}

console.log(this.dataService.serviceData); it returns "undefined".. So how can I save that data that I putted on the first component and beeing able to show it on another component? what am I missing?

UPDATE:

Some people like me didn't find an answer to the other question like this one on stack and that's because Instead of adding the providers individually you have to add them globably (on module.ts) !!!!!!!!!!!!

Alex
  • 75
  • 10
  • I marked this question as a duplicate, please ping me if not removing the services from the providers array from component doesn't work, but I suspect that's your only issue ;) – AT82 Oct 30 '17 at 07:05
  • already edited! thanks! – Alex Oct 30 '17 at 11:40

1 Answers1

1

As you said you have already added the provider on the module level,try commenting out the providers declaration from components.

Srinivas Valekar
  • 1,083
  • 10
  • 19
  • Exactly!! For people like me who didn't find an answer on the other question like this one change providers to global instead of individual. Now why does this happens? I mean why if I add them individually instead of globally it will just make an instance and if I add them globally it will work globally? --------> lol i just answered myself with the question.. quite obvious!! haha !!!! – Alex Oct 30 '17 at 11:37
  • lol.. I also faced the same issue, after struggling for 2 hours I came to know about this. – Srinivas Valekar Nov 01 '17 at 22:05