0

Whenever a checkbox is checked, I want to listen for if a key is pressed and run some code. That code will need to access the checkbox value or ngModel. I have considered something like

<input ng-repeat="task in $store.taskarr track by $index" ng-change="checked(task)" ng-model="task" type="checkbox" value="{{task}}" />

controller('checkboxController', function($scope){
    $scope.checked = function(task){
    //here goes something like a keydown-listener if task is true
        console.log(task);
    }
}).

I have to problems with my approach.

Console.log returns true/false, not the value from {{task}} as I expected. I'm not sure if I should use the ng-keydown-event or onkeydown. The event needs to be global, as more than one checkbox can be checked at the same time.

If you would prefer another approach like an attribute directive or something else, I'm all ears!

1 Answer 1

2

Try use ng-click

 <ul class="unstyled" style="list-style:none;">                                                                                                                                    
 <li ng-repeat="bill in bills">                                                                                                                                                          
   <input type="checkbox" ng-model="bill.status" ng-click="updateStatus(e, bill._id)" />                                                                                               
   <span>{{bill.description}}</span>                                                                                                                                                     
 </li>                                                                                                                                                                                   

'use strict';

angular.module('Bills')
.controller('BillCtrl', function ($scope) {
  $scope.updateStatus = function(event, id){
      alert('Registro modificado: ' + id);      
    };
 $scope.bills = [
   {'_id': '0', 'status' : false , 'description': 'Telefone'},
   {'_id': '1', 'status' : false , 'description': 'Embasa'},
   {'_id': '2', 'status' : false , 'description': 'Coelba'},
   {'_id': '3', 'status' : false , 'description': 'Internet'},
   {'_id': '4', 'status' : false , 'description': 'Carro'},
   {'_id': '5', 'status' : false , 'description': 'Seguro'}
  ];
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.