Angular checkbox and ng-click

后端 未结 4 890
慢半拍i
慢半拍i 2020-12-03 02:08

Angular 1.2:


I don\'t have the right stat

相关标签:
4条回答
  • 2020-12-03 02:52

    cardeal's answer was really helpful. Took it a little further and figured it may help others some where down the line. Here is the fiddle:

    https://jsfiddle.net/vtL5x0wh/

    And the code:

    <body ng-app="checkboxExample">
      <script>
      angular.module('checkboxExample', [])
        .controller('ExampleController', ['$scope', function($scope) {
    
        $scope.value0 = "none";
        $scope.value1 = "none";
        $scope.value2 = "none";
        $scope.value3 = "none";
    
        $scope.checkboxModel = {
            critical1: {selected: true, id: 'C1', error:'critical' , score:20},
            critical2: {selected: false, id: 'C2', error:'critical' , score:30},
            critical3: {selected: false, id: 'C3', error:'critical' , score:40},
    
           myClick : function($event) { 
              $scope.value0 = $event.selected;
              $scope.value1 = $event.id;
              $scope.value2 = $event.error;
              $scope.value3 = $event.score;
            }
        };
    
       }]);
    </script>
    <form name="myForm" ng-controller="ExampleController">
      <label>
    
    
        Value1:
        <input type="checkbox" ng-model="checkboxModel.critical1.selected" ng-change="checkboxModel.myClick(checkboxModel.critical1)">
      </label><br/>
      <label>Value2:
        <input type="checkbox" ng-model="checkboxModel.critical2.selected" ng-change="checkboxModel.myClick(checkboxModel.critical2)">
       </label><br/>
         <label>Value3:
        <input type="checkbox" ng-model="checkboxModel.critical3.selected" ng-change="checkboxModel.myClick(checkboxModel.critical3)">
       </label><br/><br/><br/><br/>
      <tt>selected = {{value0}}</tt><br/>
      <tt>id = {{value1}}</tt><br/>
      <tt>error = {{value2}}</tt><br/>
      <tt>score = {{value3}}</tt><br/>
     </form>
    
    0 讨论(0)
  • 2020-12-03 02:56

    The order of execution of ng-click and ng-model is different with angular 1.2 vs 1.6

    You must test, with 1.2 and 1.6,

    for example, with angular 1.2, ng-click get execute before ng-model, with angular 1.6, ng-model maybe get excute before ng-click.

    so you get 'true checked' / 'false uncheck' value maybe not you expect

    0 讨论(0)
  • 2020-12-03 02:59

    The order of execution of ng-click and ng-model is ambiguous since they do not define clear priorities. Instead you should use ng-change or a $watch on the $scope to ensure that you obtain the correct values of the model variable.

    In your case, this should work:

    <input type="checkbox" ng-model="vm.myChkModel" ng-change="vm.myClick(vm.myChkModel)">
    
    0 讨论(0)
  • 2020-12-03 02:59

    You can use ng-change instead of ng-click:

    <!doctype html>
    <html>
    <head>
      <script src="http://code.angularjs.org/1.2.3/angular.min.js"></script>
      <script>
            var app = angular.module('myapp', []);
            app.controller('mainController', function($scope) {
              $scope.vm = {};
              $scope.vm.myClick = function($event) {
                    alert($event);
              }
            });     
      </script>  
    </head>
    <body ng-app="myapp">
      <div ng-controller="mainController">
        <input type="checkbox" ng-model="vm.myChkModel" ng-change="vm.myClick(vm.myChkModel)">
      </div>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题