It is not possible to use functions to pass parameters directly in knockout, which will cause the function to be called at initialization time, for example:
<span style= "FONT-SIZE:14PX;" ><div data-bind= "Click:changeeditor ($index)" ></div></span>
will cause the function to be called when it is initialized, clicking on the event Changeeditor () function, which clearly violates the original intention.
There are 2 ways to implement parameter delivery:
1. Method One: Use function wrapping
<div data-bind= "event: {click:function (data, event) {changeeditor (' param1 ', ' param2 ', Data, Event)}}" > Mous E over me</div>
Click the event response function and set a layer, call the Chageeditor function, in the original Changeeditor () function call to pass in parameters.
2. Method Two: Use the BIND function
<button data-bind= "event: {click:changeEditor.bind ($data, ' param1 ', ' param2 ')}" > click me</button>
When using this method to pass parameters, the $data is formalized, cannot be changed, and can be followed by a number of parameters, such as param1,param2.
3, the parameter transfer of the exact province
<div data-bind= "event: {mouseover:myfunction}" > Mouse over me</div> <script type= "text/ JavaScript "> var viewModel = { myfunction:function (data, event) { if (event.shiftkey) { //do Something different when user have shift key down } else { //do normal action }}} ; Ko.applybindings (ViewModel);</script>
data and event two parameters are always passed by default even if the function in the binding definition does not have any parameters, and data refers to the current ViewModel object, which is the event object.
Note: When passing parameters in bind mode, data and event two parameters are still passed by default, the newly added parameters are ranked first in use , for example, the following definitions:
<span style= "FONT-SIZE:14PX;" ><div data-bind= "Click:changeEditor.bind ($data, $index)" ></div></span>
when used, index is the first parameter.
function Changeeditor (Index,data, event) {alert (' Parameters: ' + index + ', ' + data+ ', ' + event '); alert (arguments.length); var tmp = data; Tmp.headertext = ' OK!!! '; tmp.editing = true;//Columns2[idx] (TMP);}
Methods for Knockout Event pass parameters