this.property = ko.observable('value');
<!DOCTYPE html> <head> <title>KnockoutJS Observable Example</title> <script src = "https://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js" type = "text/javascript"></script> </head> <body> <!--this is called "view" of HTML markup that defines the appearance of UI--> <p>Enter your name: <input data-bind = "value: yourName" /></p> <p>Hi <strong data-bind = "text: yourName"></strong> Good Morning!!!</p> <script> <!--this is called "viewmodel". this javascript section defines the data and behavior of UI--> function AppViewModel() { this.yourName = ko.observable(""); } // Activates knockout.js ko.applyBindings(new AppViewModel()); </script> </body> </html>
<p>Enter your name: <input data-bind = "value: yourName" /> <p>
<p>Hi <strong data-bind = "text: yourName"></strong> Good Morning!!!</p>
this.yourName = ko.observable("");
Sr.No. | 读/写操作和语法 |
1 |
读取
要读取值,只需调用不带参数的 Observable 属性,例如:AppViewModel.yourName();
|
2 |
Write
要在 Observable 属性中写入/更新值,只需在参数中传递所需的值,例如:AppViewModel.yourName('Bob');
|
3 |
写入多个
多个 ViewModel 属性可以在单行中使用链接语法更新,例如:AppViewModel.yourName('Bob')。你的年龄(45);
|
this.arrayName = ko.observableArray(); // It's an empty array
this.arrayName = ko.observableArray(['scott','jack']);
alert('The second element is ' + arrayName()[1]);
Sr.No. | 方法和说明 |
1 | push('value')
在数组末尾插入一个新项目。
|
2 | pop()
从数组中移除最后一项并返回。
|
3 | unshift('value')
在数组的开头插入一个新值。
|
4 | shift()
从数组中移除第一项并返回它。
|
5 | reverse()
反转数组的顺序。
|
6 | sort()
按升序对数组项进行排序。
|
7 | splice(start-index,end-index)
接受 2 个参数-start-index 和 end-index-删除从开始到结束索引的项目并将它们作为数组返回。
|
8 | indexOf('value')
此函数返回所提供参数第一次出现的索引。
|
9 | slice(start-index,end-index)
此方法切出数组的一部分。返回从开始索引到结束索引的项目。
|
10 | removeAll()
删除所有项目并将它们作为数组返回。
|
11 | remove('value')
删除与参数匹配的项目并作为数组返回。
|
12 | remove(function(item) { condition })
移除满足条件的项目并返回它们作为一个数组。
|
13 | remove([值集])
删除与给定值集匹配的项目。
|
14 |
destroyAll()
用属性 _destroy 标记数组中的所有项目,值为 true。
|
15 |
destroy('value')
搜索与参数相等的项,并用一个特殊属性 _destroy 标记它,值为 true。
|
16 |
destroy(function(item) { condition})
查找所有满足条件的项,用属性_destroy 标记它们,并用真值标记它们。
|
17 |
destroy([set of values])
查找与给定值集匹配的项目,将它们标记为具有真值的 _destroy。
|