更改输入复选框AngularJS的状态

时间:2016-12-09 15:27:00

标签: javascript html angularjs

如何根据参数值更改checkbok状态? 示例: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() {         
    }
});

我认为问题在于参数不是布尔值。

1 个答案:

答案 0 :(得分:1)

如果您使用

,您的小提琴将起作用
ng-checked = modelValue === 1 

(假设您希望在值为1时检查它)