我是淘汰框架的新手,我想我有一个简单的问题。 我的页面获得包含邮件地址信息的AJAX结果:
["EMailAddress":"No mail address found"]
在我的html页面中,我现在要检查,如果有一个名为“EMailAddress”的attribut,并且其文本不等于“找不到邮件地址”。
它执行了以下操作:
<!-- ko if: EMailAddress-->
<!-- ko if: EMailAddress != "No mail address found" -->
<span data-bind="text: EMailAddress"></span>
<!-- /ko -->
<!-- /ko -->
你们有人能告诉我我做错了什么吗?我总是收到“找不到邮件地址”的文字!
答案 0 :(得分:0)
我的猜测是代码中的某些内容无法正常刷新或被错误地评估。即。函数(如observable)被视为属性,或者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>