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
Post a Comment