通过淘汰赛,我的局部视野中没有任何价值

时间:2012-10-11 04:37:50

标签: javascript json knockout.js asp.net-mvc-4

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>

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

1 个答案:

答案 0 :(得分: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