2016-12-09 4 views
0

Wie ändert sich der Checkbok-Status in Abhängigkeit vom Wert des Parameters? Beispiel: JSFiddleÄndern des Status der Eingabe-Checkbox AngularJS

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() {   
    } 
}); 

Ich gehe davon aus, dass das Problem liegt in der Tatsache, dass der Parameter nicht ein boolean ist.

Antwort

1

Ihre Geige wird funktionieren, wenn Sie

ng-checked = modelValue === 1 

(vorausgesetzt, Sie wollen, dass es überprüft werden, wenn der Wert 1) verwenden

+0

OMG Antwort überraschte Sie so viel Dank! – AndreyH

Verwandte Themen