1

我的 viewModel 上有一个方法,如下所示:

self.search = function () {
    $.ajax({
        url: "/api/SearchCustomers",
        data: { id: self.custTerm },
        type: "GET",
        success: function (data) {
            self.customers(data);
        }
    });
}

如果该方法已使用 knockoutvisible属性调用,我想做的是显示一个元素。那可能吗?

我已经将该方法附加到一个按钮上,如下所示:

$(document).ready(function () {
    var viewModel = new SearchCustomerViewModel();
    ko.applyBindings(viewModel);
    $("#btnSearch").click({ handler: viewModel.search });
});

我试过了,但没有用:

<div data-bind="visible: search">
    <strong><span data-bind="text: customersToShow().length"></span></strong>
    <span data-bind="">customers</span> found.
    <p>
        <label>
            Exclude Closed Accounts:
            <input data-bind="checked: excludeClosedAccs" type="checkbox" /></label>
    </p>
</div>
4

1 回答 1

1

添加一个可观察的布尔值,并使用它来指示该方法是否已被调用:

self.methodTouched = ko.observable(false);
self.search = function () {
    $.ajax({
        url: "/api/SearchCustomers",
        data: { id: self.custTerm },
        type: "GET",
        success: function (data) {
            self.customers(data);
        }
    });
    self.methodTouched(true);
}

进而:

<div data-bind="visible: methodTouched">
    <strong><span data-bind="text: customersToShow().length"></span></strong>
    <span data-bind="">customers</span> found.
    <p>
        <label>
            Exclude Closed Accounts:
            <input data-bind="checked: excludeClosedAccs" type="checkbox" /></label>
    </p>
</div>
于 2012-07-27T09:21:26.203 回答