我正在使用signalr将一行添加到表中,但我无法做到:
HTML:
<div>
<table>
<thead>
<tr>
<td>Name</td>
<td>Price</td>
</tr>
</thead>
<tbody data-bind="foreach: Products">
<tr>
<td><span data-bind="text: name"></span></td>
<td><span data-bind="text: price"></span></td>
</tr>
</tbody>
</table>
</div>
淘汰赛:
function ProductViewModel() {
Products = ko.observableArray();
Products.unshift(
{
name: Products.name,
price: Products.price
}
);
}
ko.applyBindings(new ProductViewModel());
SignalR:
chat.client.hello = function (product) {
console.log(product.name);
ProductViewModel.Product(product);
};
答案 0 :(得分:0)
看起来应该是这样的:
function ProductViewModel() {
var self = this;
self.Products = ko.observableArray();
self.AddProduct = function(product) {
self.Products.unshift(product);
};
}
var productViewModel = new ProductViewModel();
ko.applyBindings(productViewModel);
chat.client.hello = function(product) {
console.log(product.name);
productViewModel.AddProduct(product);
};