首页 > 代码库 > knockout 简单使用
knockout 简单使用
定义:
var QcViewModel = function () { var self = this; self.name = ko.observable(); self.qty = ko.observable(); }; var RecordviewModel =function() { var self = this; self.temperature = ko.observable(‘80‘); self.prodname = ko.observable(‘D成型‘); self.typename = ko.observable(‘L‘); self.ftargetqty = ko.observable(1000); self.prodqty = ko.observable(500); self.rate = ko.observable(50); self.rateless = ko.observable(50); self.Detail1 = ko.observableArray([new QcViewModel()]); self.Detail2 = ko.observableArray([new QcViewModel()]); self.Detail3 = ko.observableArray([new QcViewModel()]); }; var myViewModel = new RecordviewModel();
初始化:
ko.applyBindings(myViewModel);
修改值:
myViewModel.temperature(data.temperature); var mapping = {
‘Detail1‘: {
create: function (options) {
var newItem = new QcViewModel();
newItem.name(options.name);
newItem.qty(options.qty);
return newItem;
}
}, ‘Detail2‘: {
create: function (options) {
var newItem = new QcViewModel();
newItem.name(options.name);
newItem.qty(options.qty);
return newItem;
}
}, ‘Detail3‘: {
create: function (options) {
console.log(‘Detail3:=‘+options);
var newItem = new QcViewModel();
newItem.name(options.name);
newItem.qty(options.qty);
return newItem;
}
}
};
ko.mapping.fromJS(Detail1, mapping, myViewModel.Detail1);
ko.mapping.fromJS(Detail2, mapping, myViewModel.Detail2);
ko.mapping.fromJS(Detail3, mapping, myViewModel.Detail3);
绑定:
<span data-bind="text: temperature"></span>
遍历:
<div class="col-sm-6 col-md-6" data-bind="foreach: Detail1()">
<h2> <span style="color:red" data-bind="text: qty">90</span> <span style="color:greenyellow" data-bind="text: name"></span></h2>
</div>
knockout 简单使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。