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.

11
  • 2
    Possible duplicate of How to inject Service into class (not component) Commented May 5, 2019 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. Commented May 5, 2019 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. Commented May 5, 2019 at 14:37
  • 1
    Why couldn't it be a service? Commented May 5, 2019 at 14:39
  • 1
    I had to make a new one... try now: stackblitz.com/edit/… Commented May 5, 2019 at 15:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.