如何刷新可观察数组的属性?

时间:2013-11-15 00:02:17

标签: javascript knockout.js observable

    <div id="display" data-bind='foreach: stories'>
            <div class='story'>
                <div class="show_rait">
                    <button class="plus_btn" data-bind="click: rait++">plus</button>
                    <label data-bind="text: rait"></label>
                    <button class="minus_btn" data-bind="click: rait--">minus</button>
                </div>
                <div class='right'></div>
                <div class='title' data-bind='text: $data.title'></div>
                <div class='inside' data-bind='text: inside'></div>
            </div>
            </div>

function ViewModel() {
    var self = this;
    //data
    self.stories = ko.observableArray([
    {title: "story", inside: "some story", rait: -1},
    {title: "story", inside: "Koko jambo", rait: -1},
    {title: "story", inside: "trali vali ", rait: -1},        
    {title: "4 story", inside: "some text": -1},
    {title: "5 story", inside: "yo", rait: -1}]),

Rait ++和Rait--必须增加和删除rait。它可以工作但只是在重新加载后。我可以只刷新这篇文章<label data-bind="text: rait"></label>或我该如何解决这个问题?此外,我尝试使rait属性可观察rait: ko.observable(-1),但点击并刷新所有模板后,它返回NaN。

1 个答案:

答案 0 :(得分:0)

这只是猜测,但请试试这个:

<button class="plus_btn" data-bind="click:function(){ rait++;rait.valueHasMutated();}">plus</button>