如何将服务存储在"下拉"使用Angularjs刷新页面后

时间:2017-05-15 06:43:07

标签: angularjs

该功能我存储下拉选择值在服务中并将值从一个地方移动到另一个它工作正常,但我需要的是刷新页面下拉后将更改为选择但我需要选择值必须在下拉列表我我在这里使用cookie我附上了我的代码帮助如何做到这一点

控制器代码

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> 

2 个答案:

答案 0 :(得分:1)

您可以直接ng-modelselect绑定StoreService

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

并将StoreService存储在ng-change个活动中。

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

评论:$scope.value = StoreService;因为您正在更改$scope.value而没有任何意义,此行将覆盖您选择的空白。

ADD:

select使用ng-repeat时,您必须使用ng-selected="expression"设置默认选定的选项

<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>

注意:既然你说直接将StoreService绑定到模板导致空白页面没有错误,那么你仍然可以绑定value.storedObject

答案 1 :(得分:0)

试一试。你没有更新服务,storeservice

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