本文介绍了比较淘汰赛可观察变量的成语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题.我要检查表中单击的项目是否与model.selected不同.

I have the following problem.I want to check that the item clicked on in table is not the same as model.selected.

var model= {
items:  ko.observableArray(),
selected : ko.observable()
};

<tbody>
<!-- ko foreach: model.items -->
<tr data-bind="click:$parent.model.set_selected_item">
<td style="cursor:pointer" data-bind="varchar : title"></td>
</tr>
<!-- /ko -->
</tbody>

//ID is an observable
//selected may not be set yet - i.e an empty observable;



 var set_selected_item = function(item){
    //if item is different set 
      model.LandItem_selected(item);
      do_routine(item)
    //else
      //do nothing
    }

因为该项目是可观察的,所以永远不会为null;我如何检查可观察对象是否尚未设置?

because the item is an observable is is never null;how would I check if the observable has not been set yet?

非常感谢您的帮助.

推荐答案

在比较之前可观察到展开

Unwrap observable before comparing

var item1 = ko.observable()
console.log(ko.utils.unwrapObservable(item1))
console.log(ko.utils.unwrapObservable(item1) == null)
item1(1)
console.log(ko.utils.unwrapObservable(item1) == null)

输出

未定义

true

false

这篇关于比较淘汰赛可观察变量的成语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 02:33