AngularJS and ng-checked

0

to see if they can tell me why this happens. The thing is that I have a double ng-repeat where two lists are traversed. The first list is a list of contractors and within this list there is another list of documents. That is, the contractors have a list of documents associated with them.

To go through the two lists within the HTML I have the following code:

          <accordion close-others="true" ng-repeat="contratista in listaContratistas track by $index | filter:{activo:'S'}">
                <accordion-group heading="{{contratista.razonSocial}}">
                    <button ng-click="delContratista($index);" class="btn btn-block btn-danger btn-xs">Eliminar Contratista</button>
                    <p>Documentos obligatorios:</p>
                    <div ng-repeat="documento in contratista.documentosProyecto track by $index">
                        <div class="checkbox" ng-click="$parent.delDocumentoContratista($parent.$index, $index);" >
                        <!--<div class="checkbox" ng-click="$documento.obligatorio == 'S'? documento.obligatorio = 'N' : documento.obligatorio = 'S'" >-->
                            <label>
                                <input type="checkbox" ng-checked="listaContratistas[$parent.$index].documentosProyecto[$index].obligatorio == 'S'">
                                <!--<input type="checkbox" ng-checked="documento.obligatorio == 'S'">-->
                                {{documento.tipo}}
                            </label>
                        </div>
                    </div>
                </accordion-group>
        </accordion>

The documents that contractors have have a property called mandatory, which by default is equal to 'S'.

The problem is that when you click on a check from any contractor, your check and that of all contractors is unchecked. The code in the controller is as follows:

$scope.delDocumentoContratista = function(indexContratista, indexDocument){
            if($scope.listaContratistas[indexContratista].documentosProyecto[indexDocument].obligatorio == 'S'){
                $scope.listaContratistas[indexContratista].documentosProyecto[indexDocument].obligatorio = 'N';
            }else{
                $scope.listaContratistas[indexContratista].documentosProyecto[indexDocument].obligatorio = 'S';
            }
        }

I have already tried countless things and it still does not work. What is wrong with the code for this to happen to me?

Greetings and thank you very much.

    
asked by Enrique Rodrigo 18.12.2016 в 13:23
source

1 answer

0

You have the data contratista.documentosProyecto I imagine that these data have a id or some field that differentiates them from others. You must do the following:

In your controller creates an empty array, for example

$scope.contratistaModel = [];

Then go through your data

angular.forEach($scope.contratista.documentosProyecto, function(documento,index){
      $scope.contratistaModel[documento.id] = false;
})

With this you will be able to create an array, with the indices corresponding to each documento.id , initialized to false because it is a checkbox IF IT WAS THE CASE and it would depend on a value if the checkbox is marked or unmarked , you must do your validation, for example:

angular.forEach($scope.contratista.documentosProyecto, function(documento,index){
      if(documento..obligatorio == 'S'){
          $scope.contratistaModel[documento.id] = false;
      }else{
          $scope.contratistaModel[documento.id] = true;
      }
})

Then simply in your view:

<input type="checkbox" ng-model="contratistaModel[documento.id]" ng-checked="listaContratistas(documento)">

Here you do not send the value you have, if not the object of ng-repeat complete and call the function listaContratistas(documento) , in this function you should do this:

$scope.listaContratistas = function(documento){  
    if($scope.contratistaModel[documento.id]){

    }else{

    }
}

Here you evaluate if your UNIQUE $scope.contratistaModel[documento.id] model is checked or not, if you check in, you already know what to do.

    
answered by 19.12.2016 / 14:03
source