【问题标题】:On ng-checked i need data inside div to be displayed在 ng-checked 上,我需要显示 div 内的数据
【发布时间】:2016-02-12 05:09:40
【问题描述】:

我有一个包含“n”行的表格,每行都有一个复选框。选择复选框后,我试图显示编码在 <div> 标签内的信息。

但是即使复选框的值为false,div里面的数据还是会显示出来,我在div标签中使用ng-show来检查checkbox是真还是假。

以下是我在表格列中使用的代码:

<td>
    <input id="{{test}}" type="checkbox" value="" ng-checked="selection.indexOf(test) > -1" ng-click="toggleSelection(test)" />
</td>

在 JavaScript 中,我有以下切换功能

按索引切换给定行项目的选择

  $scope.toggleSelection = function toggleSelection(test) {
    var idx = $scope.selection.indexOf(test);    
 if it is  currently selected
     if (idx > -1) {
       $scope.selection.splice(idx, 1);
     }    
     if it is newly selected
     else {
       $scope.selection.push(test);
     }
   };

如果我做错了,请指出我,我对角度世界很陌生。

【问题讨论】:

  • 有代码示例可以分享吗?
  • 下面是我在表格列中使用的代码

标签: javascript angularjs checkbox angularjs-ng-checked


【解决方案1】:

这个例子可能非常适合你的情况

<a href="http://jsfiddle.net/t7kr8/211/" >Click here </a>

【讨论】:

    【解决方案2】:

    确保您将modelng-show 绑定

        var app = angular.module('myApp', []);
        app.controller('formCtrl', function($scope) {
          var _this = this;
          $scope.tempArr = [{
            status: false,
            data: 'hey'
          }, {
            status: false,
            data: 'hey'
          }, {
            status: false,
            data: 'hey'
          }];
          $scope.tempArr = [{
            status: false,
            data: 'hey'
          }, {
            status: false,
            data: 'hey'
          }, {
            status: false,
            data: 'hey'
          }];
        });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div data-ng-app="myApp" ng-controller="formCtrl">
      <table>
        <tr ng-repeat="item in tempArr">
          <td>
            <input type="checkbox" ng-model="item.status">
          </td>
          <td><span ng-show="item.status">{{item.data}}</span>
          </td>
        </tr>
      </table>
    </div>

    【讨论】:

      【解决方案3】:

      <div ng-app="checkbox" ng-controller="homeCtrl">
          <div ng-repeat="item in list">
              <input type="checkbox" checkbox-group />
              <label>{{item.value}}</label>
          </div>{{array}}
          <br>{{update()}}
      </div>

      var app = angular.module('checkbox', []);
      
      app.controller('homeCtrl', function($scope) {
              $scope.array = [1, 5];
              $scope.array_ = angular.copy($scope.array);
              $scope.list = [{
                  "id": 1,
                  "value": "apple",
              }, {
                  "id": 3,
                  "value": "orange",
              }, {
                  "id": 5,
                  "value": "pear"
              }];
      
              $scope.update = function() {
                  if ($scope.array.toString() !== $scope.array_.toString()) {
                      return "Changed";
                  } else {
                      return "Not Changed";
                  }
              };
      
          })
          .directive("checkboxGroup", function() {
              return {
                  restrict: "A",
                  link: function(scope, elem, attrs) {
                      // Determine initial checked boxes
                      if (scope.array.indexOf(scope.item.id) !== -1) {
                          elem[0].checked = true;
                      }
      
                      // Update array on click
                      elem.bind('click', function() {
                          var index = scope.array.indexOf(scope.item.id);
                          // Add if checked
                          if (elem[0].checked) {
                              if (index === -1) scope.array.push(scope.item.id);
                          }
                          // Remove if unchecked
                          else {
                              if (index !== -1) scope.array.splice(index, 1);
                          }
                          // Sort and update DOM display
                          scope.$apply(scope.array.sort(function(a, b) {
                              return a - b
                          }));
                      });
                  }
              }
          });

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-04
        • 2022-01-18
        • 2017-12-30
        • 1970-01-01
        • 2022-11-10
        • 2015-01-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多