如何使用AngularJS通过已检查的值进行复选框检查

How to make checkbox checked through already checked values using AngularJS?

本文关键字:检查 复选框 何使用 AngularJS      更新时间:2023-09-26

function Test1Controller($scope) {
  var storeid = window.localStorage.getItem("storeid");
    var serverData = ["Samsung Galaxy Note", "Samsung Galaxy S6", "Samsung Galaxy Avant","Samsung Galaxy Young"];
    $scope.items= [] ;
    var selectedvalue="Samsung Galaxy S6";
    for(var i=0;i<serverData.length;i++)
    {
        var modal = {
        name:serverData[i],
        selected:false
        };  
		if (selectedvalue.indexOf(serverData[i]) >= 0 || null)
		{
                        modal.selected = true;
        }
        $scope.items.push(modal);        
    }
    //----------------------------Our Shop Offers----------------------------------------
    $scope.offers = [
    {
        id: "as23456",
        Store: "samsung",
        Offer_message:"1500rs off",
        modalname: "Samsung Galaxy Young"       
    },{
        id: "de34575",
        Store: "samsung",
        Offer_message:"20% Flat on Samsung Galaxy S6",
        modalname: "Samsung Galaxy S6"       
    },
    ]
    //-----------------------------------------------------------------------------------
	$scope.selection = [];
      $scope.toggleSelection = function toggleSelection(item) {
     $scope.gotOffers=[];
      var idx = $scope.selection.indexOf(item);
      // is currently selected
      if (idx > -1) {
        $scope.selection.splice(idx, 1);
      }
      // is newly selected
      else {
        $scope.selection.push(item);
      }
        for(var i=0;i<$scope.selection.length;i++){
          for(var j=0;j<$scope.offers.length;j++){
            console.log($scope.selection[i].name  +"   "+ $scope.offers[j].modalname)
            if( $scope.selection[i].name  == $scope.offers[j].modalname){
              var idx = $scope.gotOffers.indexOf($scope.offers[j].Offer_message);
              if(idx == -1){
                console.log("inside idx")
                $scope.gotOffers.push($scope.offers[j]);
              }
            }
          }
        }
		console.log($scope.offers);
		
    };
	//---------------------------------------------------------------------------------------
     $scope.check = function()
	 
     {
	 
	 
         var checkedItems = [];
         console.log($scope.offerSelected)
            for(var i=0;i<$scope.items.length;i++)
            {
                  if($scope.items[i].selected){
                      checkedItems.push($scope.items[i].name);
                    }
            }
              console.log(checkedItems) ; 
     }
		
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<div ng-app>
  <div ng-controller="Test1Controller" >
    <div ng-repeat="item in items">
      <input type="checkbox" ng-model="item.selected"  ng-checked="selection.indexOf(item) > -1" ng-click="toggleSelection(item)"/> {{item.name}}
    </div>
    <select ng-show="gotOffers.length > 0" ng-model="offerSelected">
      <option ng-repeat="offer in gotOffers"  value="{{offer.id}}">{{offer.Offer_message}}</option>
    </select>
      <input type="button" name="submit" value="submit" ng-click="check()"/>
  </div>
</div>

这里我有两页第一页我已经选择了三星Galaxy S6,我正在移动到下一页摘要页,你可以在这里看到复选框列表1.三星Galaxy Note2.三星Galaxy S63.三星Galaxy Avant4.三星Galaxy Young

在这个列表中,我想做2.三星银河S6应该被选中。三星银河S6有一些优惠。在这个复选框列表中,三星银河S6被选中一个下拉列表,它应该显示优惠。

这是我的期望小提琴:演示但提供不工作我已经删除了这行ng-checked="selection.indexOf(item) > -1" ng-click="toggleSelection(item)"

我的原始代码:demo.in。在这个中,我想在复选框列表中推送所选的值,请帮助我完成

您可以使用item。选中属性以选中/取消选中复选框

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>   
 <div ng-app>
  <div ng-controller="Test1Controller" >
  <div ng-repeat="item in items">
  <input type="checkbox" ng-model="item.selected"  ng-checked="item.selected" ng-click="toggleSelection(item)"/>   
   {{item.name}}
 </div>
<select ng-show="gotOffers.length > 0" ng-model="offerSelected">
  <option ng-repeat="offer in gotOffers"  value="{{offer.id}}">
 {{offer.Offer_message}}</option>
  </select>
  <input type="button" name="submit" value="submit" ng-click="check()"/>
 </div>
 </div>