2016-07-05 51 views
0

我想在输入框中输入的文本应用于单击按钮上的页面。我正在使用提交绑定,但无法弄清楚我做错了什么。敲击提交绑定点击一个按钮

HTML:

<form data-bind="submit: doSomething"> 
<label> 
    <input data-bind='value: showText' /> 
</label> 
    <button type="submit">button</button> 
</form> 
    <h1><span data-bind='text: fullText'> </span></h1> 

JS:

var ViewModel = function(text) { 
    this.showText = ko.observable(text); 

    this.doSomething : function(formElement) { 

    this.fullText = ko.computed(function(){ 
     return this.showText(); 
    }, this); 
} 
}; 

ko.applyBindings(new ViewModel()); 

回答

1

第一个错误:

this.doSomething : function(formElement) { 

应该

this.doSomething = function(formElement) { 

二错误:

this.fullText 

被定义在函数内部,但你在绑定到视图模型中使用它。

我准备了带固定代码的jsfiddle