0

How to change the checkbok state depending on the value of the parameter? Example: JSFiddle

HTML:

<body  ng-app="myApp">
    <div ng-controller="rangeCtrl">
    Param:{{param}}
        <hr>
    <div>modelValue:{{modelValue}}</div>
      <input type="checkbox" style="width: 300px;"
             ng-change = "rangeChange()"
             ng-checked = "modelValue"
             ng-model = "modelValue">
    </div>
  </body>

JS:

var app = angular.module('myApp', []);
app.controller('rangeCtrl', function($scope, $interval, $timeout) {

(function update() {
    $timeout(update, 1000);
    $scope.param = Math.round((Math.random()));
  }());    
    $scope.$watch('param',
        function(newVal, oldVal) {
            if (newVal == oldVal) {
                $scope.modelValue = oldVal;
            }
        });

    $interval(function() {
        $scope.$watch('param',
            function(newVal, oldVal) {
                if (newVal !== oldVal) {
                    $scope.modelValue = newVal;
                }
            });
    }, 25);   
    $scope.rangeChange = function() {         
    }
});

I assume that the problem lies in the fact that the parameter is not a boolean.

0

1 Answer 1

1

Your fiddle will work if you use

ng-checked = modelValue === 1 

(assuming you want it to be checked when the value is 1)

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

1 Comment

OMG answer surprised Thank you so much!

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.