2015-09-21 61 views
0

Angular javascript數組是空的嗎? 我有複選框的數組數據綁定到一個角度範圍的列表 我需要檢查一些複選框,併發送它們綁定到第二角範圍的陣列:Angular javascript數組是空的?

我的HTML:

<tr ng-repeat="item in pagedItems[currentPage]" 
    ng-controller="DealerComMaintainCtrl"> 
    <td align="center"> 

    <input type="checkbox" id="{{item.IdArticle}}" 
      ng-value="item.IdArticle" 
      ng-checked="selected.indexOf(item.IdArticle) > -1" 
      ng-click="toggleSelect(item.IdArticle)" /> 

    </td> 
    <td> 
    <a href="/Dealercoms/ArticleDownload?FileName={{item.FileName}}&IdArticle={{item.IdArticle}}" 
     class="btn btn-xs btn-total-red btn-label"><i class="ti ti-download"></i></a> 
    </td> 
    <td>{{item.DateArticle | date:'dd/MM/yyyy'}}</td> 
    <td>{{item.Title}}</td> 
    <td>{{item.Archive}}</td> 
</tr> 

和js控制器:

$scope.selected = []; 

$scope.toggleSelect = function toggleSelect(code) { 
    var index = $scope.selected.indexOf(code) 

    if (index == -1) { 
    $scope.selected.push(code) 
    } else { 
    $scope.selected.splice(index, 1) 
    } 
} 

$scope.ArchiveDocs = function() { 
    var selectedoptionz = $scope.selection; 
    console.log(selectedoptionz); 
}; 

回答

0

我想你的代碼和我固定的問題,由上述ng-repeat的元件上加控制。警報僅用於檢查選定數組中的內容。這是代碼,其作品在我的系統上的罰款:

<html> 
<head> 
</head> 
<body ng-app="app"> 
<table ng-controller="DealerComMaintainCtrl"> 
    <tbody> 
     <tr ng-repeat="item in pagedItems" > 
      <td align="center"> 


       <input type="checkbox" id="{{item.IdArticle}}" ng-value="item.IdArticle" ng-checked="selected.indexOf(item.IdArticle) > -1" ng-click="toggleSelect(item.IdArticle)" /> 


      </td> 
      <td><a href="/Dealercoms/ArticleDownload?FileName={{item.FileName}}&IdArticle={{item.IdArticle}}" class="btn btn-xs btn-total-red btn-label"><i class="ti ti-download"></i></a></td> 
      <td>{{item.DateArticle | date:'dd/MM/yyyy'}}</td> 
      <td>{{item.Title}}</td> 
      <td>{{item.Archive}}</td> 

     </tr> 

    </tbody> 
    </table> 


    <!-- jQuery --> 
    <script src="./jquery-2.1.4/jquery.min.js"></script> 
    <!-- AngularJS --> 
    <script src="./angular-1.4.5/angular.min.js"></script> 
    <script> 
     var app = angular.module("app", []); 

     app.controller("DealerComMaintainCtrl", function($scope){ 
      $scope.pagedItems = [ 
       {IdArticle: 1, Title: "test1", Archive: "archiv1"}, 
       {IdArticle: 2, Title: "test2", Archive: "archiv1"}, 
       {IdArticle: 3, Title: "test3", Archive: "archiv1"}, 
       {IdArticle: 4, Title: "test4", Archive: "archiv1"} 
      ]; 
      $scope.selected = []; 

     $scope.toggleSelect = function toggleSelect(code) { 
      var index = $scope.selected.indexOf(code) 

      if (index == -1) { 
       $scope.selected.push(code) 
      } 
      else { 
       $scope.selected.splice(index, 1) 
      } 
      alert(JSON.stringify($scope.selected)); 
     } 
     }); 

    </script> 
</body> 
</html> 

試試吧,我希望它解決您的問題...

+0

你很尖銳 – Farai

+0

你是一個搖滾明星 – Farai

+0

謝謝。我很高興能幫助你 – Sim