简体   繁体   English

如何通过敲除ockout.js中的按钮来显示数字

[英]how to show a digit by clicking on button in knockout.js

I need a functionality that will do following: after clicking on a button (or div, li, etc.), in the page it should show me the value of the button (in cases of div and li it can show some data that is specific for that exact div or li) 我需要执行以下操作的功能:单击按钮(或div,li等)后,在页面中,它应该向我显示按钮的值(在div和li的情况下,它可以显示一些数据具体针对该确切的div或li)

view 视图

<ul>
<li data-bind="click: myFunction.bind($data, 'some text')">some text</li>
<li data-bind="click: myFunction.bind($data, 'some other text')">some other text</li>
</ul>
<p data-bind='text: typedDigits'></p>

viewmodel 视图模型

function AppViewModel() {
this.typedDigits = '';
this.myFunction  = function(data){

    this.typedDigits = ' ' + data;

};

ko.applyBindings(new AppViewModel());

I get the 'data' in myFunction correctly what I'm sending on click, but I don't know how to pass it to typedDigits that it showed me each time I click the button (div, li)...any ideas? 我可以在myFunction中正确获取单击时发送的“数据”,但是我不知道如何将其传递给每次单击按钮时显示给我的typedDigits(div,li)...有什么想法吗?

sincerely thanks. 衷心的感谢。

You want to make typedDigits a ko observable: 您想使typedDigits成为ko可观察的:

function AppViewModel() {
this.typedDigits = ko.observable('');
this.myFunction  = function(data){

    this.typedDigits(' ' + data);

};
}

ko.applyBindings(new AppViewModel());

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

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