Javascript敲除绑定嵌套对象无法正常工作

前端之家收集整理的这篇文章主要介绍了Javascript敲除绑定嵌套对象无法正常工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我对使用淘汰 javascript库相对较新.我在获取可观察属性时遇到问题,该属性是另一个对象的对象.这是我的代码
function Customer(id) {
    var self = this;

    self.customer_id = ko.observable(id);
    self.custnum = -1;

    self.busname = ko.observable("");
    self.address = "";
    self.city = "";
    self.state_id = "";
    self.zipcode = "";
    self.cnt_sal_id = "";
    self.cnt_first_name = "";
    self.cnt_last_name = "";
    self.cnt_title = "";

    //alert("customer: " + self.customer_id());

}


var CustomerEntryviewmodel = function(date) {
    var self = this;

    self.last_update = ko.observable(date);
    self.customer = ko.observable(new Customer(""));

    self.addCustomer = function (id) {
        var c = new Customer(id);
        self.customer = c;
        alert("New id: " + self.customer.customer_id() + " num: " + c.custnum);
    }

    self.customerSearch = function () {
    }

    self.editCustomer = function (customer_id) {
    }

    self.save = function(customer) {    
    }           
}

如何绑定到Customer对象中的属性.我尝试使用典型的javascript点表示法,如下所示:customer.customer_id

这是绑定数据的html:

<div class="field-input" style="margin-bottom:10px;">
    <input type="text" id="customer_id" style="width:100%;" 
        data-bind="jqxInput: { placeHolder: 'Customer #',value: 
                               customer().customer_id,height: 21,width: 208,minLength: 1,disabled: true }"/>
</div>

解决方法

由于客户是可观察的,因此您必须在绑定中将其展开.所以它会是这样的:
<div data-bind="text: customer().address"></div>

同样,这个

alert("New id: " + self.customer.customer_id() + " num: " + c.custnum);

将会

alert("New id: " + self.customer().customer_id() + " num: " + c.custnum);
                          //     ^ unrolled

猜你在找的JavaScript相关文章