Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing "click" binding in Knockout
    primarykey
    data
    text
    <p>I am new to Knockout , trying to learn Click binding in Knockout.</p> <p>My example code is </p> <p><strong>HTML</strong></p> <pre><code>&lt;button data-bind="click: addEmp('fnameX', 'lnameX')"&gt;Add one more&lt;/button&gt; &lt;ul data-bind="foreach: employees"&gt; &lt;li&gt; &lt;span data-bind="text: fname"&gt; &lt;/span&gt; &lt;span data-bind="text: lname"&gt; &lt;/span&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre> <p><strong>JavaScript</strong></p> <pre><code>// model function employee(fname, lname) { this.fname = ko.observable(fname); this.lname = ko.observable(lname); } // view model var vm = { company: ko.observable('Company Name'), employees: ko.observableArray(), addEmp: function (fname, lname) { this.employees.push(new employee(fname, lname)); } }; // adding employees to array vm.addEmp('fname1', 'lname1'); vm.addEmp('fname2', 'lname2'); //binding ko.applyBindings(vm); </code></pre> <p><strong>Out I was Expecting :</strong></p> <p>2 records should be displayed , and when user clicks "Add one more" , a new record should get added to viewmodel , and it should get reflected in my view.</p> <p><strong>But , the actual Output is :</strong> </p> <p>3 records are getting displayed initially , and when user clicks "Add one more" , NO record is getting added to viewmodel , and not getting reflected in the view.</p> <p>JSFiddle link : <a href="http://jsfiddle.net/Rama_Kishore/AGycj/" rel="nofollow">http://jsfiddle.net/Rama_Kishore/AGycj/</a></p> <p>Appreciate your help. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload