Zubair sadiq Zubair sadiq - 7 months ago 9
HTML Question

using angularjs want to store assets which is checked

<div style="z-index: 1; position: absolute"ng-show="ctrl.company.selected">
<div style="" ng-repeat="Asset in ctrl.company.selected.Assets">
<div class="pd-5"style="width: 300px; background-color: white; border-bottom: gray solid">
<label>
<input type="checkbox" ng-model="check"/>
{{Asset}}
</label>
</div>
<div ng-show="check">{{Asset}}</div>
</div>
</div>


above code is display asset on check now i want to store array of asset which is checked

Answer

try this, here is working fiddle

<div style="z-index: 1; position: absolute">
<div style="" ng-repeat="(index,val) in Assets">
    <div class="pd-5"style="width: 300px; background-color: white; border-bottom: gray solid">
        <label>
            <input type="checkbox" name="check[]" ng-click="addToSelectedOrNot(index,flag)" ng-true-value="true" ng-false-value="false" ng-model="flag"/>
            {{val}}
        </label>
    </div>
</div>
<div ng-show="selectedAssets.length>0">
<div>----------------Selected------------------</div>
<div>{{selectedAssets}}</div>
</div>

controller

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
$scope.Assets = ['asset1','asset2','asset3'];
$scope.selectedAssets = [];

$scope.addToSelectedOrNot = function(index,flag,val){
    if(flag == "true"){
    $scope.selectedAssets.push($scope.Assets[index]);
  }else if(flag == "false"){
    angular.forEach($scope.selectedAssets, function(selVal,selKey )
    {
     if(val == selVal){
       $scope.selectedAssets.splice(selKey,1);
     }
    });
   }
  }
}
Comments