Cat Loop Repeat Loops

0

How to create a dynamic form in the angular, this form is composed of categories and types. It was supposed to create a tab with the various categories and within each category show the types.

Below is the code example.

<div class="row">
    <div class="col-xs-12">
        <div class="box box-primary">
            <div class="box-body" id="teste" ng-app="app" ng-init="chargeData()" ng-controller="FormCtrl">
                <ul class="nav nav-tabs" role="tablist">
                    <li ng-repeat="category in categories"><a href="#{{category.ClinicalExamCategoryId}}" data-toggle="tab">{{category.clinicalExamCategoryName}}</a></li>
                </ul>
                <div class="tab-content">
                    <div class="tab-pane active" ng-repeat="category2 in categories2" id="{{category2.ClinicalExamCategoryId}}">
                        <br>
                        <div id="{{category2.ClinicalExamCategoryId}}" class="row" ng-init="chargeType(category2.ClinicalExamCategoryId);">
                            <div ng-repeat="type in types " id="{{category2.ClinicalExamCategoryId}}" class="col-xs-6">
                                <div id="{{type.ClinicalExamTypeId}}" class="box box-primary">
                                    <div class="box-header">
                                        <h3 class="box-title" id="{{type.clinicalExamTypeName}}">{{type.clinicalExamTypeName}}</h3>
                                        <div class="box-body">

                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Controller:

$scope.chargeData = function () {
             $http.get("/ClinicalExams/SelectCategory").success(function (data, status, headers, config) {
                 $scope.categories = data;
                 $scope.categories2 = data;
             //    chargeType(id);
             }).error(function (data, status, headers, config) {
                 $scope.state = "ERRO DE EXECUÇÃO";
             });
         }
         $scope.chargeType = function (id) {
             $http.get("/ClinicalExams/SelectType/"+id).success(function (data, status, headers, config) {
                 $scope.types = data;
             }).error(function (data, status, headers, config) {
                 $scope.state = "ERRO DE EXECUÇÃO";
             });
         }
    
asked by anonymous 18.12.2014 / 15:37

2 answers

1
ng-repeat="type in types";

You should use:

ng-repeat="type in types track by type.id";

or even:

ng-repeat="type in types track by $index";

But how do you use repeat within repeat; do the tests, I think you should use the first option, (track by type.id);

    
16.01.2015 / 14:01
0

Dude, honestly, I would change my service and would already bring the JSON all set up the way its menu is, with categories and subcategories in a list within each category ...

The way you do, depending on the size of your menu, you will do about 300 requests for the server.

Because you do not already have formatted so

{
    categoria: 'categoria1'
    subCategoria: [{
            desricao: 'subCategoria1'
        }]
}

That way, you do not have to call the service for each existing category, breaking your repeat within repeat would make N times easier.

    
19.12.2014 / 14:03