2

I have the following checkbox:

input(type='checkbox', ng-model='auto', ng-checked='auto', ng-change='save()')

As soon as I save it from the method in ng-change it save the auto value correctly but the app also listen to SSE (Server Sent Events) to update that value:

A simple example:

$scope.$watch(Auto.getNewValue, function (newAutoValue) {
    if (newAutoValue !== null) {
        $scope.auto = newAutoValue;
    }
});

Even if from the $watch the value is false but previously I checked it the checkbox remains checked. How can I uncheck that checkbox with AngularJS?

9
  • So when you check it, you want it to uncheck after the save? Commented Sep 4, 2015 at 13:25
  • No, only if the value from SSE return false I wish to uncheck it, same if it return true and the checkbox is not checked... I wish it to be checked... Commented Sep 4, 2015 at 13:26
  • I may be having a moment but what is SSE? Is this one of your services that you are calling from within the save function? Commented Sep 4, 2015 at 13:29
  • SSE are Server Sent Events... Real time updates coming from API in my case. SSE send messages to the client allowing me to update values in real time similar to web sockets. Commented Sep 4, 2015 at 13:32
  • Ah right yes of course. Hmm. How are you hooking this into angular though? If using a data stream or sockets are you updating a controller variable in the callback of said stream? Commented Sep 4, 2015 at 13:37

3 Answers 3

1

I solved it just adding a few more ng attributes to my input checkbox. It look like this now:

input(type='checkbox', ng-model='auto', ng-true-value='true', ng-false-value='false', ng-checked='auto', ng-change='save()')

ng-true-value and ng-false-value solved my issue.

Now everything is working fine... (even if I still don't know why... lol)

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

1 Comment

this actually works fine for me too. I tried adding ng-checked and ng-model and tried to do all manipulations. <input type="checkbox" ng-model="schedule_active" ng-true-value='true' ng-false-value='false' class="checkbox" id="schedule_active"/>
1

try below checkbox

<input type="checkbox" ng-model="yourmodel" ng-true-value='true' ng-false-value='false' class="checkbox" id="schedule_active"/>

as answer is already provided, just to have quick copy paste for others, I've added above code.

Comments

0

from https://docs.angularjs.org/api/ng/directive/ngChecked:

Sets the checked attribute on the element, if the expression inside ngChecked is truthy.

Ng checked can take an expression. So maybe create a function that returns false from whatever events you need to take place?

input(type='checkbox', ng-model='auto', ng-checked='checkedFunction()', ng-change='save()')

and then your function

function checkedFunction()
{
   //do stuff
   return false;
}

1 Comment

Nothing, it doesn't remove the checked value from the checkbox.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.