为什么计算函数this.followButtonText不会更新,总是显示(Follow),当observable this.isFollowing被更改时? $(document).ready(function(){
function AppViewModel() {
this.toggleIsFollowing = function () {
this.isFollowing = !this.isFollowing;
follow();
};
this.isFollowing = ko.observable(false);
this.followButtonText = ko.computed(function () {
return this.isFollowing ? "Unfollow" : "Follow";
});
}
ko.applyBindings(new AppViewModel());
}
});
</script>
答案 0 :(得分:2)
一个可观察的实际上是一个功能。要读取当前值,您需要将其称为不带参数的函数,如:this.isFollowing()
。
要设置observable的值,需要将新值作为第一个参数传递。因此,您的切换看起来像:this.isFollowing(!this.isFollowing());
在followButtonText
计算中,您需要将其称为函数,如:
return this.isFollowing() ? "Unfollow" : "Follow";
答案 1 :(得分:2)
更改此声明:返回this.isFollowing? “取消关注”:“关注”;
对此:返回this.isFollowing()? “取消关注”:“关注”;
括号将其称为为获得最新值所需的函数