我正在尝试根据this example键入一些与模型没有直接关系的数字。
HTML:
<input data-bind="value: screenText" />
<div>
<div>
<div data-bind="with: '1'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '2'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '3'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
</div>
<div>
<div data-bind="with: '4'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '5'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '6'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
</div>
<div>
<div data-bind="with: '7'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '8'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
<div data-bind="with: '9'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
</div>
</div>
<a>Llamar</a>
<div data-bind="with: 'hi'">
<a data-bind="text: $data,click: $root.keyPushed" href="#"></a>
</div>
JAVA:
package dew.demo.namesmodel;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.java.html.json.Model;
import net.java.html.json.Property;
import net.java.html.json.Function;
@Model(className = "Data", properties = {
@Property(name = "screenText", type = String.class)
})
class DataModel {
@Function
static void keyPushed(Data model, String keyPress) {
System.out.println(keyPress);
model.setScreenText(model.getScreenText()+keyPress);
}
static {
Data ui = new Data();
ui.setScreenText("1");
ui.applyBindings();
}
}
或许您可能已经注意到了,我的想法是将输入用作显示,并在点击时附加键值。
¿这是可能的,还是我必须采用这种方法将其强行纳入数据模型?
提前谢谢。
答案 0 :(得分:1)
在您的功能中,您使用with-binding设置$ data。因此,您希望在函数中访问$ data。这样做:
@Function
static void keyPushed(Data model, String data) {
System.out.println(data);
model.setScreenText(model.getScreenText()+ data);
}