I need to observe the variable myGlobalVar
. I need to detect changes to it and display them.
function AppViewModel() {
this.myVar = ko.observable(myGlobalVar);
}
// Activates knockout.js
ko.applyBindings(new AppViewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script>myGlobalVar = "Foo";</script>
<p>Test: <strong data-bind="text: myVar"></strong></p>
<script>
setTimeout(function() {
myGlobalVar = "Bar";
console.log("myVar set to 'Bar'. Proof:");
console.log(myGlobalVar);
}, 1000);
</script>
In the example code above I initially set window.test
to "Foo" and then after 3 seconds set it to "Bar", but the change is not getting detected.