HTML
1 | <div> |
JS
写法1: 属性法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17var viewModel = function () {
var self = this;
self.title1 = ko.observable('我就是模板标题1');
self.title2 = ko.observable('我就是模板标题2');
self.para1 = ko.observable('我是段落1');
self.para2 = ko.observable('我是段落2');
self.inputText1 = ko.observable('');
self.inputText2 = ko.observable('');
self.showModuleIndex = ko.observable(1);
self.showModule = function (index, data) {
self.showModuleIndex(index);
};
};
ko.applyBindings(viewModel(), document.getElementById('body'));写法2: extend法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18var viewModel = function () {
var self = this;
$.extend(self, {
title1: ko.observable('我就是模板标题1'),
title2: ko.observable('我就是模板标题2'),
para1: ko.observable('我是段落1'),
para2: ko.observable('我是段落2'),
inputT: ko.observable(''),
inputT: ko.observable(''),
showModuleIndex: ko.observable(1),
showModule: function (index, data) {
self.showModuleIndex(index);
},
});
};
ko.applyBindings(new viewModel(), document.getElementById('body'));