Ayeye Brazo
Ayeye Brazo

Reputation: 3476

Dynamically change checkboxes with Angularjs

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?

Upvotes: 2

Views: 3195

Answers (3)

Naveen
Naveen

Reputation: 830

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.

Upvotes: 1

Ayeye Brazo
Ayeye Brazo

Reputation: 3476

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)

Upvotes: 1

Jesse
Jesse

Reputation: 1262

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;
}

Upvotes: 0

Related Questions