mardi 15 septembre 2015

How to checked check box in angular js?

  • I have two list (List 1 and List 2) of check box in which three items available in both list. My problem is when I checked on list 1 item then list 2 item also checked not sure why. I need to stop this. My requirement is when I checked list 1 item then it does not affect List 2 item. Please help

Fiddle Here

VIEW

<div ng-app="checkbox" ng-controller="homeCtrl">
    <div ng-repeat="item in list">
        <input type="checkbox" ng-model="item.checked"  
             ng-click="fnChangeAssetType1(item)" />
        <label>{{item.value}}</label>
    </div>
    <br/><br/>
    New List<br/><br/>
    <div ng-repeat="items in lists">
      <input type="checkbox" ng-model="items.checked"  
             ng-click="fnChangeAssetType(items)"  />
        <label>{{items.value}}</label>
    </div>

</div>

JS

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

app.controller('homeCtrl', function($scope) {
       var list = [{
            "id": 1,
            "value": "apple",
        }, {
            "id": 3,
            "value": "orange",
        }, {
            "id": 5,
            "value": "pear"
        }];

     $scope.list=list;
     $scope.lists=list;

    $scope.fnChangeAssetType = function (items) {
        angular.forEach($scope.lists, function (item) {
            item.checked = false;
        });
        items.checked = true;
    };
    $scope.fnChangeAssetType1 = function (items) {
        angular.forEach($scope.list, function (item) {
            item.checked = false;
        });
        items.checked = true;
    };

    });




Aucun commentaire:

Enregistrer un commentaire