javascript - Knockout with not creating property -
i have little problem. fiddle
http://jsfiddle.net/lkqtu/25524/
js
var hellovm = function() { var vm=this; vm.obj1=ko.observable({}); vm.obj2=ko.observable({}); vm.test = function() { alert("fffff"); alert("obj 1 name "+vm.obj1().name); alert("obj 2 name "+vm.obj2().name); }; }; ko.applybindings(new hellovm(), document.getelementbyid('foo'));
html
<div id="foo"> <div> <input type="text" data-bind="value:obj1().name" /> </div> <div data-bind="with:obj2()" style="margin-top:2px"> <input type="text" data-bind="value:name" /> </div> <button data-bind="click:test">test</button> </div>
first case binding unexisting property of object directly , second case binding unexisting property of object via "with" binding
in second case(using "with") knockout doesn't create property in object. it'll created in first case.
what kind of problem have?
here jsfiddle
i hope after.
html
<div id="foo"> <div> <input type="text" data-bind="value: obj1().name" /> </div> <div data-bind="with:obj2()" style="margin-top:2px"> <input type="text" data-bind="value:name" /> </div> <button data-bind="click:test">test</button> </div>
js
the property
name
not exist , therefore can not assign value it. below have set observables have objectname
property.
var hellovm = function () { var vm = this; // added object property of name vm.obj1 = ko.observable({ name: '' }); // added object property of name = result vm.obj2 = ko.observable({ name: 'result' }); vm.test = function () { alert("fffff"); alert("obj 1 name " + vm.obj1().name); alert("obj 2 name " + vm.obj2().name); }; }; ko.applybindings(new hellovm(), document.getelementbyid('foo'));
Comments
Post a Comment