使用嵌套對象時,如何在AngularJS中創建遞歸模板?我正在嘗試從JSON對象動態構建表單,該對象包含嵌套的表單元素組: $scope.formData = [
{label:'First Name', type:'text', required:'true'},
{label:'Last Name', type:'text', required:'true'},
{label:'Coffee Preference', type:'dropdown', options: ["HiTest", "Dunkin", "Decaf"]},
{label: 'Address', type:'group', "Fields":[
{label:'Street1', type:'text', required:'true'},
{label:'Street2', type:'text', required:'true'},
{label:'State', type:'dropdown', options: ["California", "New York", "Florida"]}
]},
];我一直在使用ng-switch塊,但它對嵌套項變得難以維持,就像上面的Address對象一樣。這是小提琴:http: //jsfiddle.net/hairgamiMaster/dZ4Rg/關于如何最好地處理這個嵌套問題的任何想法?非常感謝!
3 回答

慕桂英546537
TA貢獻1848條經驗 獲得超10個贊
有一個ng-if
防止“葉子”生成不必要的ng-repeat
指令:
<script type="text/ng-template" id="field_renderer.html"> {{field.label}} <ul ng-if="field.Fields"> <li ng-repeat="field in field.Fields" ng-include="'field_renderer.html'"> </li> </ul></script><ul> <li ng-repeat="field in formData" ng-include="'field_renderer.html'"></li></ul>
這是Plunker的工作版本

江戶川亂折騰
TA貢獻1851條經驗 獲得超5個贊
可以考慮使用ng-switch來檢查Fields屬性的可用性。如果是,則為該條件使用不同的模板。此模板將在Fields數組上具有ng-repeat。
- 3 回答
- 0 關注
- 659 瀏覽
添加回答
舉報
0/150
提交
取消