2013-03-25 67 views
0

我想僅使用敲除來實現以下內容: - 我希望在單擊「button1」時使輸入字段爲空,並在「button2」被點擊。輸入字段是由它們各自的可觀察值綁定的數據。所以,我不確定我應該如何使觀察值爲null,然後在單擊button2時將其顯示回來。僅當使用敲除單擊按鈕時,輸入字段爲空

代碼:

var ViewModel = function() { 
    var self = this; 
    self.comment = ko.observable("hi there"); 
    self.message = ko.observable("hello"); 
} 

vm = new ViewModel(); 
ko.applyBindings(vm); 

我的方法:

這裏有兩種方法我試過執行,但沒有在所有的工作:

myShow: function() { 
    comment = ko.observable(""); 
}, 

myHide: function() { 
    message = ko.observable(""); 
}, 

我會明白任何幫助。

謝謝。

回答

2

你只是想臨時保存值,對吧?像這樣在視圖模型中使用私有變量。

var ViewModel = function() { 
    var self = this; 
    self.comment = ko.observable(""); 
    self.message = ko.observable(""); 

    var comment, message; 
    self.store = function() { 
     comment = self.comment(); 
     message = self.message(); 
     self.comment(""); 
     self.message(""); 
    }; 
    self.show = function() { 
     self.comment(comment); 
     self.message(message); 
    };  
}; 

這裏是the fiddle

相關問題