javascript - How to check object has any property in knockout js -
how check whether observable object has property exist in knockout js.
have tried hasownproperty
, return false me.
my code follows:
<div data-bind="click:setobject">click here</div> <div data-bind="click:init">check console</div> <script> var viewmodel = function() { var self = this; this.arrayval = ko.observable({}); this.setobject = function(){ /* have set property here */ self.arrayval({ id:10 }); }; self.init = function(){ console.log(self.arrayval()); console.log(self.arrayval.hasownproperty('id')); /* on second click (after setobject ) expect trut,but returned false */ } self.init(); }; ko.applybindings(new viewmodel()); </script>
you need obtain value inside arrayval
observable:
console.log(self.arrayval().hasownproperty('id'));
the observable has method hasownproperty
not property id
, therefore
self.arrayval.hasownproperty('id'); // false, 'id' doesn't exist on observable
while
self.arrayval().hasownproperty('id'); // true, 'id' exists on observable's value
Comments
Post a Comment