我的复选框未在angularjs中选中任何框

时间:2016-12-10 11:02:50

标签: javascript angularjs checkbox

*这里是复选框的html代码,使用复选框从数据库进行动态值初始化,但复选框没有选择任何值*

<%= f.check_box :admin, {}, 'true', 'false' %>

和js控制器代码

                <div  class="widget-body" style="display: block;">
                        <div class="widget-main">
                                <div class="table-responsive">
                                <table class="table">

                                <thead>
                                <tr>
                                <th>item</th>
                                <th>received</th>
                                    </tr>
                                </thead>

                    <tbody ng-repeat="emp in nodueaccountassets">      
        <tr>
<td>{{emp}}</td>  <td> <input type="checkbox"  ng-model="emp.selected" value="{{emp.name}}"/></td>

                             </tr>
                        </tbody>
                     </table>
                    </div>
                </div>
            </div> 

2 个答案:

答案 0 :(得分:0)

假设您想要获得所选员工,您可以这样做,

&#13;
&#13;
 var app = angular.module("app", []);

 app.controller("listController", ["$scope",
   function($scope) {
     $scope.albumNameArray = [];
     $scope.nodueaccountassets = [{
       "name": "Raymond",
       "age": 28,
       "selected": false
     }, {
       "name": "Bruce",
       "age": 96,
       "selected": false
     }, {
       "name": "Laura",
       "age": 62,
       "selected": false
     }];
     $scope.getSelected = function() {
       angular.forEach($scope.nodueaccountassets, function(emp) {
         if (emp.selected) 
         $scope.albumNameArray.push(emp.name);
       })
     }

   }

 ]);
&#13;
<!doctype html>
<html ng-app="app">

<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.min.js"></script>
  <script src="script.js"></script>
</head>

<body>
  <div ng-controller="listController">
    <div class="widget-main">
      <div class="table-responsive">
        <table class="table">
          <thead>
            <tr>
              <th>item</th>
              <th>received</th>
            </tr>
          </thead>
          <tbody ng-repeat="emp in nodueaccountassets">
            <tr>
              <td>{{emp}}</td>
              <td>
                <input type="checkbox" ng-model="emp.selected" value="{{emp.name}}" />
              </td>

            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <button ng-click="getSelected()">Get Selected</button>
    <ul>
      <li ng-repeat=" opt in albumNameArray">
        {{ opt }}
      </li>
    </ul>

  </div>
</body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

可能是您从服务器获取的以字符串格式选择emp.selected而不是布尔值的值,您获得的值如“true”,

检查你是否得到这样的响应,如果你需要将所选对象转换为布尔值。

[{
 name: 'sushil',
 selected: "true"
}]