Closed ghost closed 5 years ago
I've managed to achieve what I wanted with a basic workaround.
I extended the localStorage.setItem function in my external js file:
var baseSetItem = localStorage.setItem;
localStorage.setItem = function () {
window.dispatchEvent(new StorageEvent('storage', { key: arguments[0], newValue: arguments[1] }));
baseSetItem.apply(this, arguments);
};
and replaced the subscription @ LocalStorageService with an old fashioned listener
window.addEventListener("storage", event => { console.log(event) }, false)
For my current project there is need to pass data from a non angular control to angular. The primary application is made with Angular-Cli and thus AoT, we have client side plugins and orchestration that makes use of the web storage facility.
I have been trying to make the ng app respond to changes in the local storage.
This my configuration:
This is my listener (set up in the init routine of app main):
from an external js file, I implement the following on a click event:
When invoked,
InjectTestEvent
creates or updates the local storage item with keyngx_test_key
as intended, but this is ignored on theLocalStorageService
.To make sure the key actually exists beforehand (and to confirm the name), I created it ahead of the subscription with
The same result is achieved, I can update, set and remove this key by name from inside angular, but any change to the stored key externally is ignored.
Am I doing something wrong?