可见在Knockout.js中没有显示

时间:2016-02-02 14:49:21

标签: javascript knockout.js

我有以下ko.computed:

self.itemsInBasket = ko.computed(function(){
            return self.selectedDomains.length > 0;
        });

如果单击并递增值,则显示此内容:

this.addToCart = function(viewModel, event) {     
            console.log('click');
            self.selectedDomains(self.selectedDomains() +1);
        }

在我的html中,我使用以下数据绑定:

<div class="domains__selected" data-bind="visible:  $root.domainSearch.selectedDomains.itemsInBasket">
<strong><span class="update--domain" data-bind="text: domainSearch.selectedDomains">&nbsp;</span>&nbsp; Domains selected</strong>
</div>

它正在递增,但它没有显示计数器。

任何人都可以发现错误或指向正确的方向我会感激不尽:)

2 个答案:

答案 0 :(得分:1)

试试这个:

self.itemsInBasket = ko.computed(function(){
        return self.selectedDomains() > 0;
    });

答案 1 :(得分:1)

这个解决方案对我有用:

&#13;
&#13;
self.selectedDomains = ko.observable("");
    self.itemsInBasket = ko.computed(function () {
        return self.selectedDomains() > 0;
    });

    self.addToCart = function () {
        console.log('click');
        self.selectedDomains(self.selectedDomains() + 1);
    }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class="domains__selected" data-bind="visible:  selectedDomains() ">
    <strong><span class="update--domain" data-bind="text: selectedDomains">&nbsp;</span>&nbsp; Domains selected</strong>
</div>
<a class="btn btn-default btn-warning btn-xs"
   data-bind="click: addToCart.bind($data)">
    Update
</a>
&#13;
&#13;
&#13;