如何在“下拉菜单"中存储服务使用Angularjs刷新页面后 [英] How to store service in "dropdown" after page refreshing using Angularjs

查看:36
本文介绍了如何在“下拉菜单"中存储服务使用Angularjs刷新页面后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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> 

推荐答案

,您可以直接将 select ng-model StoreService 绑定.

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

<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 ,此行将覆盖您选择的内容空白的.

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.

添加:

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

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>

注意:由于您说的是直接在模板中绑定StoreService时导致空白页,并且没有错误,因此您仍然可以使用 value.storedObject 进行绑定.

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.

这篇关于如何在“下拉菜单"中存储服务使用Angularjs刷新页面后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆