gpt4 book ai didi

windows - 模板内虚拟元素上的 Knockout JS 数据绑定(bind)在 Windows 8.1 Cordova 应用程序上不起作用

转载 作者:可可西里 更新时间:2023-11-01 10:19:54 27 4
gpt4 key购买 nike

我在我的 Cordova 应用程序中使用以下代码进行虚拟 DOM 元素数据绑定(bind)。此代码适用于 IE 11、Android 和 iOS,但不适用于 Windows 8.1 Cordova App

如果我使用常规 DOM 元素绑定(bind),它也适用于 Windows。但我的要求是使用虚拟 DOM 元素绑定(bind)。

我们将不胜感激任何形式的帮助。

<html>
<head>
<meta charset="utf-8" />
<title>Prooduct</title>

<link href="css/index.css" rel="stylesheet" />
<script src="libs/knockout/knockout.js"></script>

<script type="text/html" id="product-template">

<ul>
<!-- ko foreach: productArray-->
<li><span data-bind="text: productName"></span><br /></li>
<!-- /ko -->
</ul>

</script>
</head>
<body>

<h4>Product List</h4>

<div data-bind="template: { name: 'product-template'}"></div>

<script type="text/javascript">
function ProductViewModel() {
this.productArray = ko.observableArray([
{ productName: 'Milk' },
{ productName: 'Oil' },
{ productName: 'Butter' }]);
}
ko.applyBindings(new ProductViewModel());
</script>
</body>
</html>

最佳答案

我使用 cordova CLI 4.3.1/Visual Studio 2015/Windows 10 和 Windows 8.1(远程调试)测试成功:

index.js:

// and then run "window.location.reload()" in the JavaScript Console.
$(document).ready(function () {

document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );

function onDeviceReady() {
// Handle the Cordova pause and resume events
document.addEventListener( 'pause', onPause.bind( this ), false );
document.addEventListener( 'resume', onResume.bind( this ), false );

// TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.

};

function onPause() {
// TODO: This application has been suspended. Save application state here.
};

function onResume() {
// TODO: This application has been reactivated. Restore application state here.
};

function AppViewModel() {
var self = this;
self.productArray = ko.observableArray([
{ productName: 'Milk' },
{ productName: 'Oil' },
{ productName: 'Butter' }]);

self.removeProduct = function () {
self.productArray.remove(this);
}
};
var vm = new AppViewModel();
ko.applyBindings(vm);
});

index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>testios</title>

<!-- testios references -->
<link href="css/index.css" rel="stylesheet" />
</head>
<body>
<h2>List of product</h2>
<hr />
<ul>
<!-- ko foreach: productArray -->
<li>
<span data-bind="text: productName"></span> <a href="#" data-bind="click: $parent.removeProduct">Remove </a>
</li>
<!-- /ko -->
</ul>
<!-- Cordova reference, this is added to your app when it's built. -->
<script src="scripts/jquery-2.1.4.js"></script>
<script src="scripts/knockout-3.3.0.js"></script>
<script src="cordova.js"></script>
<script src="scripts/platformOverrides.js"></script>

<script src="scripts/index.js"></script>
</body>
</html>

初始窗口:

Initial window

点击 Remove Oil 后:

After click on remove Oil

你用的是最新的knockout-3.3.0.js吗?

在我看来,您也可以在没有 cordova 的情况下使用 IE 对其进行测试,因为它是相同的浏览器引擎:

http://jsfiddle.net/k7x7a6q9/

在 Windows 10 下的 IE11 和 MS Edge 20.10240.16384.0 中测试

找到根本原因的一个好方法是查看 DOM-Explorer,看看发生了什么。它会在您开始调试时自动打开,或者您可以在调试期间在 Visual Studio 右上角的“快速启动”字段中键入“DOM”来打开它:

enter image description here

...并在 JavaScript 中设置断点,以验证是否抛出点击事件:

enter image description here

关于windows - 模板内虚拟元素上的 Knockout JS 数据绑定(bind)在 Windows 8.1 Cordova 应用程序上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31674567/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com