我有多個視圖模型,我正在頁面上運行,以解決任何潛在的衝突,我將父節點添加到基本應用綁定文本。然而,我遇到的其中一個問題是,我無法再使用這些節點之外的屬性。我試圖找出是否有任何方法來更新視圖模型綁定之外的單個html節點,我也綁定了該模型。Knockoutjs適用於多個html節點
<html>
<head>
<script>
function VM1() {
this.ViewModelProp1 = ko.observable(1);
this.NotficationsNumber = ko.observable(1);
}
function VM2() {
this.ViewModelProp2 = ko.observable(1);
}
ko.applyBindings(VM1, document.getElementById('vm1'));
ko.applyBindings(VM1, document.getElementById('vm2'));
</script>
</head>
<body>
<!-- I want this property applied to the VM1 -->
<h1 id="notifiy" data-bind="text: NotficationsNumber"></h1>
<p id="vm1">
<strong data-bind="text: ViewModelProp1"></strong>
</p>
<p id="vm2">
<strong data-bind="text: ViewModelProp2"></strong>
</p>
</body>
</html>
只需創建一個稍微複雜視圖模型。 – Andrei
你解決了嗎?請閱讀答案 –