kero
Version:
41 lines (33 loc) • 1.66 kB
Plain Text
<div class="example-content"><!-- ko通过data-bind绑定数据 -->
<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<h2>Hello, <span data-bind="text: fullName"> </span>!</h2></div>
<div class="example-content ex-hide"><script>// 定义ViewModel
var ViewModel = function(first, last) {
// ko.observable可实时监听数据,实现绑定
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);
this.fullName = ko.pureComputed(function() {
// ko.pureComputed用于执行计算,实时返回改变后的结果
return this.firstName() + " " + this.lastName();
}, this);
};
ko.applyBindings(new ViewModel("Planet", "Earth")); // 通过ko.applyBindings执行knockout
</script></div>
<div class="examples-code"><pre><code><!-- ko通过data-bind绑定数据 -->
<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<h2>Hello, <span data-bind="text: fullName"> </span>!</h2></code></pre>
</div>
<div class="examples-code"><pre><code>// 定义ViewModel
var ViewModel = function(first, last) {
// ko.observable可实时监听数据,实现绑定
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);
this.fullName = ko.pureComputed(function() {
// ko.pureComputed用于执行计算,实时返回改变后的结果
return this.firstName() + " " + this.lastName();
}, this);
};
ko.applyBindings(new ViewModel("Planet", "Earth")); // 通过ko.applyBindings执行knockout</code></pre>
</div>