Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Binding knockoutjs'foreach'绑定无法获取定义为类的属性?_Binding_Foreach_Knockout.js - Fatal编程技术网

Binding knockoutjs'foreach'绑定无法获取定义为类的属性?

Binding knockoutjs'foreach'绑定无法获取定义为类的属性?,binding,foreach,knockout.js,Binding,Foreach,Knockout.js,我对使用Knockoutjs是新手 我的viewModel定义如下: // this is a subclass var Order = { selfdata: { id : ko.observable(), goodsName : ko.observable(''), color : ko.observable(''), size : ko.observable(''),

我对使用Knockoutjs是新手

我的viewModel定义如下:

// this is a subclass
var Order = {
        selfdata: {
            id : ko.observable(),
            goodsName : ko.observable(''),
            color : ko.observable(''),
            size : ko.observable(''),
            count: ko.observable(''),
            orderDate: ko.observable(''),
            remarks: ko.observable('')
        },
    }
   // this is a viewModel
   function ShoppingCar() {
        var self = this;
        self.orders = ko.observableArray([]);
        self.show = function (msg) {
            return msg.id;
        }

        //init
        $.get('/Shopping/GetOrders', null, function (data) {
            var items = $.map(data, function (item) { return Order.init(item) });// I write a init function to init order
            self.orders(items);
        });
    }
//then I use Knockoutjs foreach binding as below:
<ul data-role="listview" data-theme="e" data-divider-theme="d">
 <!-- ko foreach: orders -->
     <li data-role="list-divider" data-bind="text: $data.selfdata.id">//!I get null value here</li>

     <li><a href="#">
        <h3 data-bind="text: $data.isValid"></h3>
        <p><strong data-bind="text: $data.selfdata.color">//! here has null value too!</strong></p>
        <p class="ui-li-aside"><strong>12:14</strong>PM</p>
        </a></li>
 <!-- /ko --> 

所以,我的问题是,如何在ko的foreach中显示order.selfdata?

您是否尝试过将以下内容放在下面:

selfdata.id

另外,您是否已调试并确保实际正确填充了Orders对象?

您需要关闭强标记。它被这些评论抹去了

<p><strong data-bind="text: $data.selfdata.color">//! here has null value too!</strong></p>
应该是

<p><strong data-bind="text: $data.selfdata.color"></strong></p>
您也不需要$data。selfdata.color应该可以正常工作,假设数据实际上被正确地传递到可观察对象中


如果修复注释和删除$data不起作用,我将使用浏览器调试器检查并查看可观察对象中的实际数据。

非常感谢!我在chrome中调试,发现了一个愚蠢的错误。当我从服务器获取json时,属性名是Id,name。。。但在js中,我使用id、名称……哦,我的上帝!