โซลูชันที่จะทำงานกับหลาย ๆ ขอบเขตและหลายตัวแปรภายในขอบเขตเหล่านั้น
บริการนี้มีพื้นฐานมาจากคำตอบของ Anton แต่สามารถขยายได้มากขึ้นและสามารถทำงานได้ในหลาย ๆ ขอบเขตและอนุญาตให้เลือกตัวแปรขอบเขตหลายตัวในขอบเขตเดียวกัน มันใช้เส้นทางเส้นทางเพื่อจัดทำดัชนีแต่ละขอบเขตจากนั้นชื่อตัวแปรขอบเขตเพื่อทำดัชนีให้ลึกลงหนึ่งระดับ
สร้างบริการด้วยรหัสนี้:
angular.module('restoreScope', []).factory('restoreScope', ['$rootScope', '$route', function ($rootScope, $route) {
var getOrRegisterScopeVariable = function (scope, name, defaultValue, storedScope) {
if (storedScope[name] == null) {
storedScope[name] = defaultValue;
}
scope[name] = storedScope[name];
}
var service = {
GetOrRegisterScopeVariables: function (names, defaultValues) {
var scope = $route.current.locals.$scope;
var storedBaseScope = angular.fromJson(sessionStorage.restoreScope);
if (storedBaseScope == null) {
storedBaseScope = {};
}
// stored scope is indexed by route name
var storedScope = storedBaseScope[$route.current.$$route.originalPath];
if (storedScope == null) {
storedScope = {};
}
if (typeof names === "string") {
getOrRegisterScopeVariable(scope, names, defaultValues, storedScope);
} else if (Array.isArray(names)) {
angular.forEach(names, function (name, i) {
getOrRegisterScopeVariable(scope, name, defaultValues[i], storedScope);
});
} else {
console.error("First argument to GetOrRegisterScopeVariables is not a string or array");
}
// save stored scope back off
storedBaseScope[$route.current.$$route.originalPath] = storedScope;
sessionStorage.restoreScope = angular.toJson(storedBaseScope);
},
SaveState: function () {
// get current scope
var scope = $route.current.locals.$scope;
var storedBaseScope = angular.fromJson(sessionStorage.restoreScope);
// save off scope based on registered indexes
angular.forEach(storedBaseScope[$route.current.$$route.originalPath], function (item, i) {
storedBaseScope[$route.current.$$route.originalPath][i] = scope[i];
});
sessionStorage.restoreScope = angular.toJson(storedBaseScope);
}
}
$rootScope.$on("savestate", service.SaveState);
return service;
}]);
เพิ่มรหัสนี้ไปยังฟังก์ชั่นการทำงานของคุณในโมดูลแอพของคุณ:
$rootScope.$on('$locationChangeStart', function (event, next, current) {
$rootScope.$broadcast('savestate');
});
window.onbeforeunload = function (event) {
$rootScope.$broadcast('savestate');
};
ฉีดบริการ restoreScope เข้าไปในคอนโทรลเลอร์ของคุณ (ตัวอย่างด้านล่าง):
function My1Ctrl($scope, restoreScope) {
restoreScope.GetOrRegisterScopeVariables([
// scope variable name(s)
'user',
'anotherUser'
],[
// default value(s)
{ name: 'user name', email: 'user@website.com' },
{ name: 'another user name', email: 'anotherUser@website.com' }
]);
}
ตัวอย่างข้างต้นจะเริ่มต้น $ scope.user เป็นค่าที่เก็บไว้มิฉะนั้นจะใช้ค่าเริ่มต้นเป็นค่าที่ให้และบันทึกไว้ หากหน้าถูกปิดรีเฟรชหรือเปลี่ยนเส้นทางค่าปัจจุบันของตัวแปรขอบเขตที่ลงทะเบียนทั้งหมดจะถูกบันทึกไว้และจะถูกกู้คืนในครั้งต่อไปที่มีการเยี่ยมชมเส้นทาง / หน้า
sessionStorage.restoreState
"true"
สำหรับการแก้ปัญหาที่ถูกต้องที่จะยังคงมีข้อมูลเมื่อรีเฟรชหน้าให้ดูที่นี่ สำหรับข้อมูลที่คงอยู่เมื่อเส้นทางเปลี่ยนไปให้ดูที่คำตอบของ carloscarcamo สิ่งที่คุณต้องทำก็คือใส่ข้อมูลลงในบริการ