简体   繁体   English

剔除绑定仅适用于单向

[英]Knockout-Binding only works One-Way

I'm using knockout with typescript for building the UI of my application. 我正在使用带敲除功能的打字稿来构建应用程序的UI。 I want to use a knockout binding to notify my typescript if something in my UI changes. 如果UI发生更改,我想使用敲除绑定来通知我的打字稿。

My code works, but unfortunately only in one-way. 我的代码可以运行,但不幸的是只能单向运行。 The initial value 5 is shown after page was loaded, but if I change something in the breakTime input, the subscribe-method is never entered. 加载页面后显示初始值5,但是如果我在breakTime输入中进行了更改,则永远不会输入subscribe-method。

The selectedDates array contains objects which contain the breakTime properties. selectedDates数组包含包含breakTime属性的对象。

The HTML code: HTML代码:

<!-- ko 'foreach': $root.selectedDates-->
<td>
    <input type="text" name="breakTime" data-bind="value: breakTime()" class="bookingTextBox" />
</td>
<!-- /ko -->

The typescript code: 打字稿代码:

public breakTime: KnockoutObservable<string>;

constructor(date: Date, viewModel: Booking.BookingViewModel) {    
    this.breakTime = ko.observable("30");
    this.breakTime.subscribe((newValue) => {
        alert(newValue);
    });
}

I never worked with knockout foreach-element before, maybe the error is caused by that. 我以前从未使用过敲除foreach-element,也许错误是由那引起的。 Do I have to do something else ot get this working? 我是否需要做其他事情才能使它正常工作?

You need to remove parens from the value binding data-bind="value: breakTime" 您需要从值绑定data-bind="value: breakTime" 删除括号

The reason is that if you use parens your are binding to the input the result of the observable at the moment that the binding is loaded. 原因是,如果使用parens,则将绑定到输入,这是在加载绑定时观察到的结果。 Is exactly the same if you do this data-bind="value: 5" . 如果您执行此data-bind="value: 5"则完全相同。

Without parens you are binding to value the whole observable. 没有parens,您将约束整个可观察值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM