Note that there are some explanatory texts on larger screens.

plurals
  1. POAngular - Knockout model sharing
    primarykey
    data
    text
    <p>I want to bind Angular model to a Knockout component. It's working - when the Angular model changes the Knockout binding applies. But the problem is in the opposite direction - I want the Angular model to update when the Knockout component changes, and do so without touching the component (it mustn't know about the Angular wrapper). The target - using Angular to build a rapid prototyping framework around KO components, which are being used in other environments too. The KO components can and should share models (which come from the Angular wrapper) therefore this question.</p> <p>EDIT: Here is a <a href="http://jsfiddle.net/HmcnB/10/" rel="nofollow">jsFiddle</a> example to showcase what I'm trying to achieve. Its a simplification because in real-world KO components will use internal VM's which will be difficult to $watch. But even here I don't get why the $watch doesn't work.</p> <pre><code>var sharedData = { personName: "alex", personAge: "32" }; function WrapperCtrl($scope){ $.each(sharedData, function(key, value) { sharedData[key] = (typeof value !== "function") ? ko.observable(value) : value; }); $scope.wrapData = sharedData; ko.applyBindings(sharedData, document.getElementById("ko_1")); ko.applyBindings(sharedData, document.getElementById("ko_2")); $scope.$watch( function () { return sharedData.personName(); }, function(newValue, oldValue) { console.log("change"); } ); $scope.doSomething = function(){ console.log("before angular function: ", $scope.wrapData.personName(), $scope.wrapData.personAge()) sharedData.personName('Bob').personAge(41); console.log("after angular function: ", $scope.wrapData.personName(), $scope.wrapData.personAge()) }; } function doSomething() { console.log("before knockout function", sharedData.personName(), sharedData.personAge()) sharedData.personName('Mary').personAge(25); console.log("after knockout function", sharedData.personName(), sharedData.personAge()) } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload