如何在函数内设置一个observable?

时间:2014-08-04 06:11:12

标签: javascript knockout.js

我正在尝试将一个observable传递给一个函数。

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());

2 个答案:

答案 0 :(得分:1)

你需要传递observable本身,你当前只传递它的值。使用

this.registerClick = toggleContent(this.content);

而不是

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

答案 1 :(得分:0)

如果你绑定" registerClick"参加活动,不要传递任何价值。

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

希望它有所帮助。