I have a service that I am running to retrieve device information, and I want to copy that back to the clipboard once it has been retrieved. I have the following code:
callServiceToCopy() {
let stringData = '';
let thingName: string = this.route.snapshot.params['thingName'];
let curDevice = this.devicesService
.get(thingName)
.subscribe((data) => {
this.device = data;
stringData = JSON.stringify(data);
console.log(stringData);
this.copy(stringData);
console.log("Copy Complete");
});
}
onCopyFailure() {
alert('Failed to copy to clipboard user ');
}
copy(text) {
console.log(text + ' sent to copy');
this._clipboardService.copyFromContent(text);
}
If I have the __clipboardService outside of the callback, it is able to copy text to the clipboard. However, If I have it in the callback, it does not (and thus, does not have the device data). I did follow the Plunker information to get this up and running, and looked at the following question for guidance, but it falls a little show of my needs.