0

The function i stored the dropdown selected value in service and move the value from one place to another it working fine, but what i need is after refreshing the page dropdown will change to select but i need select value must be in dropdown i am using cookie also here i attached my code help how to do this

Controller code

app.factory('StoreService', function() {
  return {
    storedObject:''
  };      
});

app.controller('UserNameShowCtrl', ['$scope', '$http', '$window', '$filter', '$cookieStore','StoreService', 
 function ($scope, $http, $window, $filter, $cookieStore,StoreService) {


$scope.FacilityDropValues=[ { value: 0, content: "First content" },
            { value: 1, content: "Second content" }]

   $scope.value=  $cookieStore.get('FacilityID');

        $scope.FacilityChange=function(){
            debugger;
                $scope.value = StoreService;
                $cookieStore.put("FacilityID", StoreService);
                 $cookieStore.put("FacilityID1", $scope.value.storedObject);

        };

html

<select id="MaterialFacility"  
     class="form-control"   
     ng-change="FacilityChange(value.storedObject)"
     ng-model="value.storedObject" >

   <option value=''>Select</option>
   <option ng-repeat="FacilityDropValue in FacilityDropValues" 
       value="{{FacilityDropValue.value}}">{{FacilityDropValue.content}}</option>                                           
</select> 
AT82
  • 71,416
  • 24
  • 140
  • 167
jose
  • 1,044
  • 1
  • 12
  • 35

2 Answers2

1

you can bind ng-model of select with StoreService directly.

<select id="MaterialFacility" 
   class="form-control"   
   ng-change="FacilityChange()"
   ng-model="StoreService.storedObject" >

and store StoreService at ng-change event.

$scope.FacilityChange=function(){
  debugger;
  // $scope.value = StoreService;
  $cookieStore.put("FacilityID", StoreService);
  $cookieStore.put("FacilityID1", StoreService.storedObject);
};

Comments: $scope.value = StoreService; doesn't make any sense since you are changing $scope.value, this line will overwrite what you have selected with blank.

ADD:

when using ng-repeat at select, you have to use ng-selected="expression" to set the default selected option

<select id="MaterialFacility"  
        class="form-control"   
        ng-change="FacilityChange(value.storedObject)"
        ng-model="value.storedObject" >

  <option value=''>Select</option>
  <option ng-repeat="FacilityDropValue in FacilityDropValues" 
          ng-selected="FacilityDropValue.value === value.storedObject"
          value="{{FacilityDropValue.value}}">{{FacilityDropValue.content}}</option>                                           

</select>

NOTE: since you are saying when binding StoreService directly in template leads to blank page and no error, here you san still bind with value.storedObject.

Pengyy
  • 37,383
  • 15
  • 83
  • 73
0

Try this out. you did not update the service, storeservice

Instead of 
 $scope.value = StoreService;
Use 
StoreService.storedObject =$scope.value.storedObject