我正在尝试在JavaScript ViewModel中实现输入字段和字段之间的双向绑定.绑定已声明连接.不幸的是,我在UI中所做的更改并没有反映在我的ViewModel中.
我的代码看起来像那样(由于我这里没有代码而写在我脑海中)
视图:
<form data-win-bind="onsubmit: onCalculate">
<div class="field">
Product Name:
<input type ="number" data-win-bind="text:value1"/>
</div>
<div class="field">
Product Price:
<input type ="number" data-win-bind="text:value2"/>
</div>
<div class="field">
Result
<br />
<span data-win-bind="innerText: result" />
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
JavaScript的
var model= WinJS.Class.define(
function() {
this.onCalculate = calculate.bind(this);
this.value1 = 0;
this.value2 = 0;
this.result = 0;
},{
value1: 0,
value2: 0,
result: 0
calculate: function() {
this.result = this.value1 + this.value2;
return false;
}
}, {});
// Make the model Observable
var …Run Code Online (Sandbox Code Playgroud)