with-binding不适用于ko.computed()

时间:2013-08-23 10:29:54

标签: javascript knockout.js

我正在努力使用ko.computed-function和with binding。 当按下“新建”按钮时,我想显示一个空的输入文本字段。 从下拉列表中选择时,我希望文本字段从下拉列表中显示所选的optionsText。

这是来自viewModel.js:


// selected item from ticket drop down
self.selectedItem = ko.observable(null);

// status of button "New"
self.newIsPushed = ko.observable(false);

self.showName = ko.computed(function () {
        // logic for calculating when to show Name. 
        return (self.newIsPushed() && (self.selectedItem() ? (null) : true));
    }, self);

来自我的ascx文件:


<select data-bind="options: ticketCollection, 
                   optionsCaption: 'Choose...', 
                   optionsText: 'ticketname', 
                   value: selectedItem"></select> 

<button type="button" name="btnNew"
   data-bind="click: newTicket, disable: newIsPushed">New</button> 

<p data-bind="with: showName"> 
   <label>Name: <input type="text" 
   data-bind="value: ticketname" /></label><br />
</p>  

当我按下“新建”按钮或从我的ddl中选择另一个项目时,为什么输入字段没有更新?

干杯!

阿斯勒:)

1 个答案:

答案 0 :(得分:0)

<p data-bind="with: showName">替换为<p data-bind="with: selectedItem">