0

Outside of Angular project, there is a TS class from which I'm trying to send object to angular component via service.

sender.ts is TS class which sends object using Angular service;

export class Sender {

  injector;
  servicePointer;
  myData: Object;

  constructor() {

    this.injector = Injector.create({
      providers: [
        {
          provide: ShareDataService,
          deps: []
        },
      ]
    });

    this.servicePointer = this.injector.get(ShareDataService);
  }

  public send() {
    this.myData = {
      name: "Robert",
      visible: true
    }

    return this.servicePointer.dataVisibility(this.myData);
  }
}

share-data.service.ts is an Angular service which successfully receives object from sender.ts class:

@Injectable({
  providedIn: 'root'
})
export class ShareDataService {

  changeDataSource = new BehaviorSubject<Object>({});

  public dataUpdate$ = this.changeDataSource.asObservable();

  public dataVisibility = (dataInfo: Object) => {
    //console.log(dataInfo);
    this.changeDataSource.next(dataInfo);
  }
}

app.component.ts includes the Sender TS class and calls .send() method:

export class AppComponent implements OnInit {
  private foo:Sender.Sender = new Sender.Sender();

  constructor() {}

  ngOnInit() {
    this.foo.send();
  }
}

At least, there is a receive component which subscribes to the service:

export class ReceiverComponent implements OnInit {
  constructor(private shareDataService: ShareDataService) { }

  receivedData;

  ngOnInit() {
    this.shareDataService.dataUpdate$.subscribe(success => {
      this.receivedData = success;
      console.log(this.receivedData);
    })
  }
} 

and always returns empty object from the service, although object with data comes to the service (console.log(dataInfo)).

What could cause the problem? In the service there is

@Injectable({
   providedIn: 'root'
})

decorator with root application injector and in the sender class there is also an injector from service is provided. Could that might the problem? If so, is there another way to inject the service? There is also a stackblitz with code explained above.

corry
  • 1,457
  • 7
  • 32
  • 63
  • 2
    Possible duplicate of [How to inject Service into class (not component)](https://stackoverflow.com/questions/41432388/how-to-inject-service-into-class-not-component) – Qortex May 05 '19 at 14:07
  • Why do you use `new` to get a reference to the Sender, and why do you use the injector to get an instance of ShareDataService in the sender? Sender should just be a service and use dependency injection. – JB Nizet May 05 '19 at 14:29
  • @JBNizet I'm using `new` because of the instantiation of the class and injector is used because constructor injection is undefined. In the project Sender represents a class and cannot be a service. – corry May 05 '19 at 14:37
  • 1
    Why couldn't it be a service? – JB Nizet May 05 '19 at 14:39
  • I tried to tranform it to the service and result is the same [stackblitz](https://stackblitz.com/edit/class-component-communication-service-wfeclk) – corry May 05 '19 at 15:00
  • Just make it a service like your shared service... https://stackblitz.com/edit/class-component-communication-service-wfeclk?file=src%2Fapp%2Fapp.component.ts I didn't look closely at the code, just changed the `Sender` service to a service... so I don't take responsibility if there is something that could be improved :D – AT82 May 05 '19 at 15:05
  • @AJT_82 Do you mean on change `Sender` class to a service :)? That's exactly what I've done in [stackblitz](https://stackblitz.com/edit/class-component-communication-service-wfeclk) – corry May 05 '19 at 15:10
  • Sorry, linked the wrong stackblitz :D I don't know what kinda attempt you made to make it a service. It should look just like your other service: https://stackblitz.com/edit/class-component-communication-service-wfeclk?file=src%2Fapp%2Fapp.component.ts – AT82 May 05 '19 at 15:15
  • ...hmm... doesn't let me fork it correctly... – AT82 May 05 '19 at 15:16
  • 1
    I had to make a new one... try now: https://stackblitz.com/edit/class-component-communication-service-v5tgg6?file=sender%2Fsender.ts – AT82 May 05 '19 at 15:20
  • @AJT_82 The problem was in the sender instance... I've just made instantiation in the constructor of the AppComponent like you did :) – corry May 05 '19 at 15:28

0 Answers0