I was wondering if there was a way I could have a ng-click function which takes in a scope variable execute after the value of the ng-model of that same scope variable changes. I have a list of analysis
objects in a list called analyses
that have (among others) an include
boolean attribute. They are displayed in html as button checkboxes to press to include/exclude.
I have this html:
Number of bisulfite sequences: {{included}} used / {{excluded}} excluded
<div ng-repeat="analysis in analyses"><br><label class="btn btn-success"
ng-model="analysis.include" ng-click="set(analysis.include)" btn-checkbox>
This is the code in the controller:
$scope.analyses = some_list_of_analyses
$scope.included = 0
$scope.excluded = $scope.analyses.length
$scope.set = function(include){
//more code to execute involving $scope.analyses
if(include){
$scope.included += 1
$scope.excluded -= 1
}
else{
$scope.excluded += 1
$scope.included -= 1
}
}
right now, assuming I start with a list of analyses that are all excluded as you can see in the initialization, when I click on a button, it should increase the included
scope variable by one and decrease the excluded
by one. instead, it passes in analysis.include
before the variable actually changes so the first button I click shows -1 used / n+1 excluded.
The solutions I can think of are either to find some way of having ng-click execute after the scope variable is bound after ng-model, or some way to change $scope.analyses objects within the ng-click method, so then the ng-model statement isn't needed?
Thanks!
EDIT: the ng-model statement is actually needed for the btn-checkbox directive. I need this directive to show a toggle on/off for including/excluding certain analyses. Alternative directives or code to implement css for indicating inclusion/exclusion would be helpful!
What if instead of passing in the analysis.included you get it from the scope in your function
I created a plunkr to demonstrate
basically HTML:
So here, you pass in the $index so you know which one to look at on $scope in the controller. so then in your controller:
It seemed to work in the plunkr. the included went up and the excluded went down
Update: New Plunkr that toggles included/excluded instead of only incrementing.
As well as updated set function: