pass variable from html to controller in angularjs

1

Hello, everyone presented a new challenge and I need help from you, it turns out that I need to send a data to the controller to use it:

<div ng-repeat="model in modulos track by $index"> <div style="float:left; padding-right:100px;"> <div class="can-toggle demo-rebrand-2"> <input id="{{ id[$index] }}" type="checkbox" ng-model="ValueModulo"> <label for="{{ id[$index] }}"> <div class="can-toggle__switch" data-checked="Yes" data-unchecked="No"></div> <div class="can-toggle__label-text"><p style="font-size:15px; width:100px;">{{model}}</p></div> </label> </div> </div> </div>

ok within the div , there is a ng-repeat that shows switches according to the number that exist (varies) and is accompanied by an id which by track by $index I assign it to its respective module, now I want to send this id to the controller, especially only one, that is when you activate the switch to be able to use it there ...

How can I send it?

in the controller I am using the following code to make the changes:

Conection.CheckModulo({

   modulo_id: IDRECIBIDO
  }, function(respuesta) {

    var data = JSON.parse(JSON.stringify(respuesta));
    var activo = data.activo;

  if(activo === "1"){

    $scope.ValueModulo = true;
  }

  else if(activo === "0"){

     $scope.ValueModulo = false;
  }

  else {

    console.log('Error de respuesta');
  }
});

  $scope.$watch('ValueModulo', function(val) {
    if (!angular.isUndefined(val)) {
        if (val) {
            Conection.OnPreferencial({
                modulo_id: IDRECIBIDO
            });
        } else {
            Conection.OffPreferencial({
                modulo_id: IDRECIBIDO
            });
        }
    }
});

'

Here I ask about the status of the switch, and depending on that it is shown on or off,

and with the $ scope.watch active or deactivate the switch

    
asked by Hernan Humaña 19.12.2016 в 20:51
source

2 answers

2

I join the response of @ManuelObregozo and add the following:

As you have your controls within a ng-repeat ALL will have the same ng-model , what I advise you is to do the following:

<input type="checkbox" ng-model="id[$index]" ng-change="switchUpdated($index)">

As you see I put ng-model="id[$index]" and from the controller you can get the value of that switch

$scope.switchUpdated = function(index){
    if($scope.id[index]){
        //Si el switch es true
    }else{
       //Si esta en false.
    }
}
    
answered by 19.12.2016 / 21:13
source
1

You could use the ng-change directive

<div ng-repeat="model in modulos track by $index">
             <div style="float:left; padding-right:100px;">
             <div class="can-toggle demo-rebrand-2">
             <input id="{{ id[$index] }}" type="checkbox" ng-model="ValueModulo" ng-change="switchUpdated($index)">
              <label for="{{ id[$index] }}">
               <div class="can-toggle__switch" data-checked="Yes" data-unchecked="No"></div>
               <div class="can-toggle__label-text"><p style="font-size:15px; width:100px;">{{model}}</p></div>
               </label>
               </div>
             </div>
           </div>

Then in your controller you would have something like:

$scope.switchUpdated = function(index){
  [...]
}
    
answered by 19.12.2016 в 21:00