Angular モデルを Knockout コンポーネントにバインドしたいと考えています。機能しています - Angular モデルが変更されると、Knockout バインディングが適用されます。しかし、問題は逆方向にあります。Knockout コンポーネントが変更されたときに Angular モデルを更新し、コンポーネントに触れずに更新する必要があります (Angular ラッパーについて認識してはなりません)。ターゲット - Angular を使用して、他の環境でも使用されている KO コンポーネントの周りにラピッド プロトタイピング フレームワークを構築します。KO コンポーネントはモデル (Angular ラッパーに由来する) を共有できますし、共有する必要があるため、この質問です。
編集:これは、私が達成しようとしていることを示すjsFiddleの例です。実際の KO コンポーネントは $watch が難しい内部 VM を使用するため、単純化されています。しかし、ここでも $watch が機能しない理由がわかりません。
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())
}