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