0

我是淘汰赛框架的新手,我想我有一个简单的问题要问你。我的页面得到一个包含邮件地址信息的 AJAX 结果:

["EMailAddress":"No mail address found"]

在我的 html 页面中,我现在想检查是否有一个名为“EMailAddress”的属性,以及它的文本是否不等于“未找到邮件地址”。

它做了以下不起作用:

<!-- ko if: EMailAddress-->
    <!-- ko if: EMailAddress != "No mail address found" -->
        <span data-bind="text: EMailAddress"></span>
    <!-- /ko -->
<!-- /ko -->

你们谁能告诉我我做错了什么?我总是显示“未找到邮件地址”的文字!

4

1 回答 1

0

我的猜测是您的代码中的某些内容没有正确刷新或被错误地评估。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>     

于 2013-10-04T11:39:41.383 回答