Can I pass a variable in a template binding?

THE JOATMON picture THE JOATMON · Dec 6, 2013 · Viewed 10.2k times · Source

I know this isn't a good method to use long term, but for troubleshooting, is there any way I can pass a simple string while binding a template and then access it as a variable within the template? For instance, if this was my binding:

<!-- ko template: { name: tmplOne }, myvar: 'apple' -->

and this was tmplOne:

<div>
    <span>Fruit: </span>
    <span data-bind="text: myvar"></span>
</div>

It would result in the folowing:

fruit: apple

Even if I have to declare an observable in the viewmodel called "fruit", can I manually set it at template binding?

Answer

Ainsof picture Ainsof · Jun 5, 2015

Use

<!-- ko template: { name: tmplOne, templateOptions: {myvar: 'apple'} } -->

More here: http://www.knockmeout.net/2011/03/quick-tip-reusing-template-by-passing.html