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.