2016-07-05 48 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