2

My view has

<input type="checkbox" class="check_box" ng-model="campaign.paused"  
       ng-click="CampaignPauseClicked(campaign, $event)" />

<p>campaign.paused == {{campaign.paused}}</p>                           

with the <p> being for debugging. It shows false, as it shoudl, given the data, but in the controller

$scope.CampaignPauseClicked = function(campaign, $event)
{
    campaign.paused = ! campaign.paused;

when I breakpoint on the first code line, the value of campaign.paused is true (!).

I have searched the code and campaign.paused is not being written elsewhere.

Any idea what could be happening here?


[Update] I am using an ng-click fucntion, which I have not shown in its entirity, because I need it to "swallow" the $event and prevent it from propogating to the parent.

3
  • since you are doing campaign.paused = ! campaign.paused; its normal for campaign.paused to be true Commented Apr 24, 2016 at 13:43
  • I am sorry, but I don't understand your comment (my bad). That statement is supposed to toggle the current value. Do you see some wrong in it? Commented Apr 24, 2016 at 15:44
  • 1
    its simple. when you are clicking on the checkbox your model ng-model="campaign.paused" gets the value of true this is done automatically because of Angular. What you do in your code is to reverse that and have true when the checkbox is not clicked and false when it is. It looks that you need to read on the ng-model directive docs.angularjs.org/api/ng/directive/ngModel Commented Apr 24, 2016 at 15:54

2 Answers 2

2

That's because ng-model is updating the value when you click the checkbox. You're undoing what Angular is doing for you.

If you want to do it by yourself in your $scope.CampaignPauseClicked function, remove the ng-model part from the html.

Otherwise, you can let Angular do its thing, leave the ng-model="campaign.paused" clause, and remove the first line from your ng-click callback.

Sign up to request clarification or add additional context in comments.

1 Comment

Lolx!! How could I make such a clueless n00b mistake?? Of course, your correct (+1 and the answer). There is no need for me to toggle the checked state, since the ng-model is doing that for me. How embarrassing :-/
1

Also you can replace the ng-click with the ng-change directive since you are using a checkbox. ng-change will run everytime the checkbox state is changed (checked/unchecked)

<input type="checkbox" class="check_box" ng-model="campaign.paused"  
       ng-change="CampaignPauseChanged ($event)" />

<p>campaign.paused == {{campaign.paused}}</p>  

And in your controller:

$scope.CampaignPauseChanged = function(event)
{
    console.log(campaign.paused)
    console.log(event)
}

Another option would be the ng-checked directive here is an example in this plunker. As you can clearly see the checkbox model returns true only if it is checked.

1 Comment

Thanks (+1). I apoliogze because I did not explain that I need to use ng-clck - I have now updated the question to explain why.

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.