简体   繁体   中英

Name of current value in Knockout

I'm getting started with Knockout. I can't figure out how to setup up the binding in the valuesTemplate. What is the name of the current value of the iterator?

<div id="knock">
    <p>Name: <input data-bind="value: Name" /></p>
    <table>
        <thead><tr><td>Value</td></tr></thead>
          <tbody data-bind="template: {name: 'valuesTemplate', foreach: Values}" />
    </table>

    <script type="text/html" id="valuesTemplate"> 
        <tr><td><input data-bind="value: value" /></td></tr> 
    </script>
</div>

<script type="text/javascript">
    var data = {"Name":"Fish","Values":["Cod","Salmon","Perch","Glish"],"Id":"lookups/1"};
    var viewModel = ko.mapping.fromJS(data);
    ko.applyBindings(viewModel);
</script>

Inside of a jQuery Template, you can access the current context by using $data or $item.data , so like:

<script type="text/html" id="valuesTemplate"> 
     <tr><td><input data-bind="value: $data" /></td></tr> 
</script>

However, if you are using observables, you are going to want your values array to contain objects rather than just strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM