页面刷新后或在angularjs中移至另一页面后保留下拉列表值 [英] Retain the dropdown value after the page is refresh or move to another page in angularjs
问题描述
让我解释一下我的情况.我的主页上有下拉列表.如果更改了下拉列表,则根据下拉列表值更改数据.
如果我刷新页面或移至另一页面,则下拉列表将自动清除.
let me explain my scenario. I am having the dropdownlist in my master page. if changed dropdownlist the data are changed depend upon the dropdownlist value.
If i refreshed my page or moved to another page the dropdownlist will clear automatically.
在刷新页面或移至另一页面后,我想保留下拉列表的值.
I want to retain the dropdownlist value after refresh the page or moved to another page.
我尝试过这样,但这无济于事.
I tried like this but this is not help ful.
HTML
<select id="Facility_ID" required typeof="text" name="Facility Name" ng-options="Role.FacilityName for Role in Roles"
form="DistanceMatrixFormId" class="form-control" ng-model="Role._id" ng-selected="getFacilityID(Role._id.FacilityName)">
</select>
Controller.js
Controller.js
$scope.getFacilityID = function (data) {
localStorage.setItem("FacilityName", data);
var getfacility = localStorage.getItem("FacilityName");
}
我不知道如何保留价值.谁能固定我.
I don't know how to retain the value. can any one fixed me.
推荐答案
您不能将对象放入本地存储中,必须将字符串存储在本地存储中.
You cannot put an object into the local storage, you must store strings inside local storage.
If you want, you can have an implementation I did here : How to add local storage in angular?
对于您当前的代码,您无需使用ng-selected. ng-model就足够了.
For your current code, you don't need to use ng-selected. ng-model is enough.
<select id="Facility_ID" required typeof="text"
name="Facility Name"
ng-options="Role.FacilityName for Role in Roles"
form="DistanceMatrixFormId"
class="form-control"
ng-model="Role._id"
ng-change="updateLocalStorage()">
</select>
在角度控制器内部,您可以执行以下操作:
And inside your angular controller, what you can do is the following :
myApp.controller('controllerName', ['$scope', 'LocalStorageService',
function($scope, LocalStorageService){
// After initialization of your "Role" object
$scope.Role._id = LocalStorageService.retrieve('mySelectValue');
$scope.updateLocalStorage = function(){
LocalStorageService.store('mySelectValue', $scope.Role._id);
}
}])
这篇关于页面刷新后或在angularjs中移至另一页面后保留下拉列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!