我的猜测是您的代码中的某些内容没有正确刷新或被错误地评估。IE。函数(如可观察对象)被视为属性,或者 viewModel 中的值根本不是可观察对象。但是如果不看完整的代码就很难判断。
这是一个快速工作示例,可以完成您尝试做的事情,这可能会有所帮助......
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"></script>
</head>
<body>
<!-- ko if: validEmail() -->
<span data-bind="text: email"></span>
<!-- /ko -->
<button onclick="pretendNotFoundAjax()">Simulate Not Found</button>
<button onclick="pretendSuccessfulAjax()">Simulate Found</button>
<script>
var viewModel = {
email : ko.observable(""),
validEmail : function() {
var emailValue = this.email();
return emailValue && emailValue != "No mail address found";
}
};
$(function(){
ko.applyBindings(viewModel);
});
function pretendNotFoundAjax() {
// imagine we've already performed an ajax call and the result was not found, which we set in the xhr callback in a way similar to the below line of code
viewModel.email("No mail address found");
}
function pretendSuccessfulAjax() {
// imagine we've already performed an ajax call and the result was successful, we got a valid email, which we set in the xhr callback in a way similar to the below line of code
viewModel.email("validemail@domain.com");
}
</script>
</body>