lundi 6 juin 2016

Angularjs $scope with parameter

I'm new in Angular and I can't achieve something really simple, lets say we have several checkbox input loops followed by an input text field like this:

<body ng-app="ngToggle">
    <div ng-controller="AppCtrl">
        <input type="checkbox" ng-repeat="btn in btns" ng-model="changing" ng-change="change(btn.value, 'other')" class="here" value="">

        <input type="text" class="uncheck" ng-disabled="other">


    <!-- Here comes the other one -->

       <input type="checkbox" ng-repeat="btn in btns" ng-model="changing" ng-change="change(btn.value, 'other2')" class="here" value="">

       <input type="text" class="uncheck" ng-disabled="other2">
    </div>
</body>

And the idea in here is this: once the checkbox with value "other" is checked it will enable the corresponding text field determinate in the field parameter of the change function, so I develop a function change(value, field) and works like this:

     $scope.btns = [
        {value:'one'},
        {value:'two'},
        {value:'other'}
    ];

   $scope.other = true;
   $scope.other2 = true;

   $scope.change = function(value, field) {
    if(value == 'other'){
        if($scope.field == false){
        $scope.field = true;
      } else {
        $scope.field = false;
      }
    }
  };

But the problem is, this does not work, I don't know if I making this in the right way, the $scope.field is the problem, how can I pass a parameter to the $scope

Hope someone can help me. Thanks.




Aucun commentaire:

Enregistrer un commentaire