你好溢出者!
我遇到了将数据绑定到foreach循环内的对象的问题。现在我正在使用typeahead来获得自动完成功能。当我这样做时,我可以将特定对象的Id保存在变量中。我想将此Id添加到产品对象中。
问题是将特定ID附加到特定产品。
用户可以根据需要添加或删除具有自动完成功能的产品/输入字段。
function Product() {
var self = this;
self.id = ko.observable(null);
self.name = ko.observable(); };
var dataWithID = 12345; //this id comes from another function, will change.
//TODO: Attach the specific id to the product object.
var ViewModel = function () {
var self = this;
self.selected = ko.observable();
self.products = ko.observableArray([new Product()]);
self.add = function () {
self.products.push(new Product());
} };
ko.applyBindings(new ViewModel());
/ J
答案 0 :(得分:1)
将它推入像这样的产品
function Product(id) {
var self = this;
self.id = ko.observable(id);
self.name = ko.observable();
};
var ViewModel = function () {
var self = this;
self.selected = ko.observable();
self.products = ko.observableArray([new Product()]);
self.add = function () {
self.products.push(new Product(dataWithID)); // <-- pass in here
}
};