0
var baseUri = '@ViewBag.ApiUrl';
var viewmodel = function () {
    var self = this;
    self.VoucherDetails = ko.observableArray([]);
    $.getJSON(baseUri, this.VoucherDetails);
    alert(self.VoucherDetails);
};


<table>
    <tbody data-bind='foreach: VoucherDetails'>
        <tr>
            <td data-bind="text : $data.empcode">
                <span data-bind=" text : $data.empcode"></span> test
            </td>
            <td data-bind=" text : empcode">
                <span data-bind=" text : empcode"></span>
            </td>

        </tr>
    </tbody>
</table>

这是我的部分观点,empcode 没有任何价值,或者$data.empcode,我是新手knockoutjs。我的代码有什么问题?

4

1 回答 1

0

您的代码看起来不完整:

  • $.getJson 从不设置 VoucherDetails 的值(不传递回调)。
  • 你永远不会调用 ko.applyBindings

它应该是:

var baseUri = '@ViewBag.ApiUrl';
var viewmodel = function () {
    var self = this;
    self.VoucherDetails = ko.observableArray([]);
    $.getJSON(baseUri, function(data){
        //create your VoucherDetails here based on the data (push each item to VoucherDetails using $.each
   });  
};

ko.applyBindings(viewModel);

http://knockoutjs.com/documentation/observableArrays.html

于 2012-10-11T04:44:33.200 回答