簡體   English   中英

如何在函數內部設置可觀察的?

[英]How to set an observable inside a function?

我試圖將一個可觀察的傳遞給一個函數。

function toggleContent(switcher) {
  if (switcher == false) {
    alert(switcher); //this shows the value
    switcher(true);  //this creates an error
    switcher = true; //this doesn't do anything
  }
}

this.content = ko.observable(false);
this.registerClick = toggleContent(this.content());

您需要傳遞可觀察對象本身,而當前僅傳遞其值。 采用

this.registerClick = toggleContent(this.content);

代替

this.registerClick = toggleContent(this.content());

如果將“ registerClick”綁定到事件,請不要傳遞任何值。

this.registerClick = function(){
    console.log(arguments); // you can get anything expected.
};

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM