0

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>

Here i have two pages 1st page itself i have selected Samsung Galaxy S6 and i am moving to next page summary page here you can see list of checkbox 1.Samsung Galaxy Note 2.Samsung Galaxy S6 3.Samsung Galaxy Avant 4.Samsung Galaxy Young

in this list i want to make 2.Samsung Galaxy S6 should be checked.and Samsung Galaxy S6 have some offers .in that checkbox list Samsung Galaxy S6 is checked one dropdown should come it should show offers .

this is my expectation fiddle : demo but offer not working i have removed this line ng-checked="selection.indexOf(item) > -1" ng-click="toggleSelection(item)"

my orginal code : demo.in this i want to push that selected value in that checkbox list pls help me out

1
  • This may not answer for your question, for checkbox instead of ng-click use ng-change. Commented Dec 21, 2015 at 14:26

1 Answer 1

1

You can use item.Selected property to check/uncheck your checkbox

<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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.