Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在函数中设置一个可观察值?_Javascript_Knockout.js - Fatal编程技术网

Javascript 如何在函数中设置一个可观察值?

Javascript 如何在函数中设置一个可观察值?,javascript,knockout.js,Javascript,Knockout.js,我试图把一个可观测的传递给一个函数 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.observab

我试图把一个可观测的传递给一个函数

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.
};
希望能有帮助