knockout實現巢狀迴圈繫結
阿新 • • 發佈:2019-02-09
實現效果如下:
程式碼:
<script type="text/javascript" src="knockout-2.2.0.js"></script> <ul data-bind="foreach: { data: categories, as: 'category' }"> <li> <ul data-bind="foreach: { data: items, as: 'item' }"> <li> <span data-bind="text: category.name"></span>: <span data-bind="text: item"></span> </li> </ul> </li> </ul> <script> var viewModel = { categories: ko.observableArray([ { name: 'Fruit', items: ['Apple', 'Orange', 'Banana'] }, { name: 'Vegetables', items: ['Celery', 'Corn', 'Spinach'] } ]) }; ko.applyBindings(viewModel); </script>