How to specify the name property using knockout js

How to specify the name property using knockout js

Asked on December 1, 2018 in knockout js.
Add Comment


  • 3 Answer(s)

       Specify the name property by using the attr binding in a template or foreach like this assuming the phone numbers are in an observable array.

    <input data-bind='attr: { name: "Phones["+$index()+"].Name"}' />
    

       If using the new version of knockout then use the $index.

       It permit  to name the inputs so they can work with MVC model binding.

    Answered on December 1, 2018.
    Add Comment

        Use the  ‘uniqueName‘, build in knockout. See the documentation: http://knockoutjs.com/documentation/uniqueName-binding.html

        If the custom binding is wanted then use custom binding: http://knockoutjs.com/documentation/custom-bindings.html

        These is suitable answer for primitive name and primitive Id : Unique ids in knockout.js templates and Knockout JS “uniqueName” binding – Same name to two fields

        See this code for unique id generation :

    ko.bindingHandlers.uniqueId = {
        init: function (element, valueAccessor) {
            if (valueAccessor()) {
                element.id = ko.bindingHandlers.uniqueId.prefix + (++ko.bindingHandlers.uniqueId.counter);
            }
        },
        counter: 0,
        prefix: "unique_id_"
    };
    
    Answered on December 1, 2018.
    Add Comment

       Make a custom binding which set the name attribute.

        Look at this demo:

    http://knockoutjs.com/documentation/custom-bindings.html

        Then use this code:

    <input data-bind='yourBindingName: Name' />
    
    Answered on December 1, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.