淘汰赛 "You cannot apply bindings multiple times to the same element"
Knockout Js "You cannot apply bindings multiple times to the same element"
我正在使用 kendo 移动应用程序构建器,我正在使用 knockout js 进行绑定,但出现错误“您不能将绑定多次应用于同一元素 ”。我有两个包含绑定的 javascript 文件,在我的代码下方
//Employee.js//
function EmployeeViewModel() {
this.EmployeeName= ko.observable();
this.EmployeeMobile= ko.observable();
this.EmployeeEmail= ko.observable(); }
ko.applyBindings(new EmployeeViewModel());
//Company.js//
function CompanyViewModel() {
this.CompanyName= ko.observable();
this.CompanyMobile= ko.observable();
this.CompanyEmail= ko.observable(); }
ko.applyBindings(new CompanyViewModel());
//In index page i am using this both script file drag and drop//
<html>
<head>
</head>
<body>
<script src="Employee.js"></script>
<script src="Company.js"></script>
</body>
</html>
多次应用绑定。您需要先清除绑定。
喜欢下面
var element = $('#elementId')[0];
ko.cleanNode(element);
然后只有您可以在同一元素上再次应用绑定。
"ko.applyBindings" 函数有两个参数:
applyBindings(viewModelOrBindingContext, rootNode);
首先 - 查看模型
第二个 - DOM 将应用绑定的节点
您调用 "ko.applyBindings" 方法两次 - 在两个函数中,仅使用第一个参数。这意味着您要将两个不同的模型绑定到同一个节点 - 文档根目录。这会导致错误。
您可以使用两种方法:
创建一个带子模型的全局视图模型并仅应用绑定一次:
//Employee.js//
function EmployeeViewModel() {
this.EmployeeName= ko.observable();
this.EmployeeMobile= ko.observable();
this.EmployeeEmail= ko.observable();
}
//Company.js//
function CompanyViewModel() {
this.CompanyName= ko.observable();
this.CompanyMobile= ko.observable();
this.CompanyEmail= ko.observable();
}
//In index page i am using this both script file drag and drop//
<html>
<head>
</head>
<body>
<script src="Employee.js"></script>
<script src="Company.js"></script>
<script>
ko.applyBindings({ employee: new EmployeeViewModel(), company: new CompanyViewModel() });
</script>
</body>
</html>
将绑定应用于不同的节点:
```
//Employee.js
function EmployeeViewModel() {
this.EmployeeName= ko.observable();
this.EmployeeMobile= ko.observable();
this.EmployeeEmail= ko.observable();
ko.applyBindings(new EmployeeViewModel(), document.getElementById("employee"));
}
//Company.js
function CompanyViewModel() {
this.CompanyName= ko.observable();
this.CompanyMobile= ko.observable();
this.CompanyEmail= ko.observable();
ko.applyBindings(new CompanyViewModel(), document.getElementById("company"));
}
//In index page i am using this both script file drag and drop//
<html>
<body>
<script src="Employee.js"></script>
<script src="Company.js"></script>
<div id="employee"></div>
<div id="company"></div>
</body>
</html>
```
我正在使用 kendo 移动应用程序构建器,我正在使用 knockout js 进行绑定,但出现错误“您不能将绑定多次应用于同一元素 ”。我有两个包含绑定的 javascript 文件,在我的代码下方
//Employee.js//
function EmployeeViewModel() {
this.EmployeeName= ko.observable();
this.EmployeeMobile= ko.observable();
this.EmployeeEmail= ko.observable(); }
ko.applyBindings(new EmployeeViewModel());
//Company.js//
function CompanyViewModel() {
this.CompanyName= ko.observable();
this.CompanyMobile= ko.observable();
this.CompanyEmail= ko.observable(); }
ko.applyBindings(new CompanyViewModel());
//In index page i am using this both script file drag and drop//
<html>
<head>
</head>
<body>
<script src="Employee.js"></script>
<script src="Company.js"></script>
</body>
</html>
多次应用绑定。您需要先清除绑定。
喜欢下面
var element = $('#elementId')[0];
ko.cleanNode(element);
然后只有您可以在同一元素上再次应用绑定。
"ko.applyBindings" 函数有两个参数:
applyBindings(viewModelOrBindingContext, rootNode);
首先 - 查看模型
第二个 - DOM 将应用绑定的节点
您调用 "ko.applyBindings" 方法两次 - 在两个函数中,仅使用第一个参数。这意味着您要将两个不同的模型绑定到同一个节点 - 文档根目录。这会导致错误。
您可以使用两种方法:
创建一个带子模型的全局视图模型并仅应用绑定一次:
//Employee.js// function EmployeeViewModel() { this.EmployeeName= ko.observable(); this.EmployeeMobile= ko.observable(); this.EmployeeEmail= ko.observable(); } //Company.js// function CompanyViewModel() { this.CompanyName= ko.observable(); this.CompanyMobile= ko.observable(); this.CompanyEmail= ko.observable(); } //In index page i am using this both script file drag and drop// <html> <head> </head> <body> <script src="Employee.js"></script> <script src="Company.js"></script> <script> ko.applyBindings({ employee: new EmployeeViewModel(), company: new CompanyViewModel() }); </script> </body> </html>
将绑定应用于不同的节点:
```
//Employee.js
function EmployeeViewModel() {
this.EmployeeName= ko.observable();
this.EmployeeMobile= ko.observable();
this.EmployeeEmail= ko.observable();
ko.applyBindings(new EmployeeViewModel(), document.getElementById("employee"));
}
//Company.js
function CompanyViewModel() {
this.CompanyName= ko.observable();
this.CompanyMobile= ko.observable();
this.CompanyEmail= ko.observable();
ko.applyBindings(new CompanyViewModel(), document.getElementById("company"));
}
//In index page i am using this both script file drag and drop//
<html>
<body>
<script src="Employee.js"></script>
<script src="Company.js"></script>
<div id="employee"></div>
<div id="company"></div>
</body>
</html>
```