2016-12-10 66 views
0

*這裏是html代碼我想獲取來自數據庫的複選框值,但我的複選框未選中任何值,只有複選框顯示,但沒有選擇任何值。 ...我的複選框沒有在angularjs中選擇任何框

 ng-controller="noduesaccountsmodalcontroller" ng-init="init()"> 


    <form name="accounts" ng-submit=submit(accounts) novalidate> 
<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> 
      {{albumNameArray}} 

    <!-- <input type="checkbox" value="{{emp}}" ng-model="checked" ng-init="checked=true"{{emp}}><br /> -->              


    <button type="submit" value="submit" class="btn btn-primary">ACCEPT</button> 
     <button class="btn btn-warning" ng-click="popup.$rollbackViewValue();">REJECT</button> 

JS控制器代碼爲

  application.controller('noduesaccountsmodalcontroller',function ($scope,$http,$window,$modal,$filter)                                      
      $scope.nodueaccountassets=data.list; 
     /*alert($scope.nodueaccountassets)*/ 
       }) 
      }) 

$scope.submit=function(form){ 
    $scope.albumNameArray = []; 

angular.forEach($scope.nodueaccountassets,function(emp){ 
    if (emp.selected) $scope.albumNameArray.push(emp.name); 

    alert(emp.selected); 


/*$scope.albumNameArray = $scope.nodueaccountassets.filter(function(emp){ 
     return emp.selected; 

     alert(emp.selected);*/ 
    }) 
    /*var emp_data='emp_assets='+$scope.nodueaccountassets+'&accounts_comments='+$scope.empcomments+'&emp_code='+$scope.emplycode; 
    alert("data is"+emp_data) 

    $http.get(domain+'/insertaccountassets?'+emp_data) 
    .success(function(data, status, headers, config){ 
    alert('submit successfully'); 
       }) 

.error(function(data, status, headers, config){ 
      alert(data); 
      }) 
      alert("error while submitting") 
      }  
     $scope.reject=function(form) 
     { 
$modal.dismiss('reject'); 
var emp_data='accounts_comments='+$scope.empcomments+'&  emp_code='+$scope.emplycode; 
      alert(emp_data) 
      $http.get(domain+'/insertaccountassets?'+emp_data) 
     }*/ 
} 

}); 
+0

你解決了嗎? –

+0

nop我的問題是這個項目是基於春天MVC的角度複選框值是動態值,但我的複選框沒有選擇任何框意味着不選擇甚至任何框和數據綁定 –

回答

0

我已經從您提供的代碼中提取了plunker,並對您的HTML和JS代碼進行了一些調整。

確保這樣,當你創建.selected屬性數組對象,並指定爲ng-model你的複選框得當

炒鍋在HTML的JSON數據有數組中的對象

<!DOCTYPE html> 
<html> 

    <head> 
    <script data-require="[email protected]*" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script> 
    <script data-require="[email protected]" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script> 
    <link rel="stylesheet" href="style.css" /> 
    <script src="script.js"></script> 
    </head> 

    <body ng-app="app" ng-controller="noduesaccountsmodalcontroller" ng-init="init()"> 
    <form name="accounts" ng-submit="submit(accounts)" novalidate=""> 
     <table class="table"> 
     <thead> 
      <tr> 
      <th>item</th> 
      <th>received</th> 
      </tr> 
     </thead> 
     <tbody ng-repeat="emp in nodueaccountassets"> 
      <tr> 
      <td>{{emp.name}}</td> 
      <td> 
       <input type="checkbox" ng-model="emp.selected" value="{{emp.name}}" /> 
      </td> 
      </tr> 
     </tbody> 
     </table> 

     {{selectedItems}} 


       <!-- <input type="checkbox" value="{{emp}}" ng-model="checked" ng-init="checked=true"{{emp}}><br /> --> 
     <button type="submit" value="submit" class="btn btn-primary">ACCEPT</button> 
     <button class="btn btn-warning" ng-click="popup.$rollbackViewValue();">REJECT</button> 
    </form> 
    </body> 

</html> 

在JS

var app = angular.module('app',[]); 
app.controller('noduesaccountsmodalcontroller',function($scope){ 

    $scope.nodueaccountassets = [{'name':'x'},{'name':'a'}, 
    {'name':'b'},{'name':'c'},{'name':'d'}]; 

    $scope.init= function(){ 

    }; 
    $scope.selectedItems =[]; 
    $scope.submit = function(acc){ 
     angular.forEach($scope.nodueaccountassets,function(emp){ 
      if(emp.selected){ 
       $scope.selectedItems.push(emp.name); 
      } 
      }); 
    }; 
});