php - How to use dynamic observable variable names in html files in knockoutjs -


i using knockjs , have created dynamic observablearrays in js file. ex. product+productid creates dynamic observablearrays product123.

i want use in data bind foreach loop , want create variable dynamically again in html file.

something : data-bind="foreach: { data: "product"+product.id()()}

so "product"+product.id()() binding should call product123() array.

how can achieve this?

you can use $data refer current context, , use array notation index dynamically-named element.

vm = {    product: ko.observable('123'),    product123: ko.observablearray([      'one', 'two', 'three'    ])  };    ko.applybindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>  <div data-bind="foreach: $data['product'+product()]">    <div data-bind="text: $data"></div>  </div>


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -