admin管理员组文章数量:1405534
What's the general (if there is any) consensus on storing view states as part of the URL in Angularjs
and how would I go about doing it? I have a fairly plex view/route with many filters to set, tabs, etc which result in a view state.
I see the advantage for storing the state of all these view ponents as part of the URL in an easier navigation within the application (navigating back
would restore the previous view with all selections made without loading the state from the server, which would be an alternative). Another advantage is that the view state bees bookmarkable.
Is there a pattern that I can use for guidance? Has anyone done this before and can share some experiences? Should I stay away from storing view states in the URL?
What's the general (if there is any) consensus on storing view states as part of the URL in Angularjs
and how would I go about doing it? I have a fairly plex view/route with many filters to set, tabs, etc which result in a view state.
I see the advantage for storing the state of all these view ponents as part of the URL in an easier navigation within the application (navigating back
would restore the previous view with all selections made without loading the state from the server, which would be an alternative). Another advantage is that the view state bees bookmarkable.
Is there a pattern that I can use for guidance? Has anyone done this before and can share some experiences? Should I stay away from storing view states in the URL?
Share Improve this question asked Nov 16, 2013 at 7:06 orangeorange 8,11215 gold badges87 silver badges154 bronze badges1 Answer
Reset to default 5If the required data can be seriaslized into an { key
, value
}, then you can use $location.search()
to save and retrieve this information in your controllers:
app.controller("myCtrl", function ($scope, $location, $routeParams) {
// Load State
var savedState = $location.search();
allProperties.forEach(function (k) {
if (typeof savedState[k] !== 'undefined') {
$scope.model[k] = savedState[k];
} else {
$scope.model[k] = defaultValues[k];
}
});
// Save the parameters
$scope.createUrlWithCurrentState = function() {
allProperties.forEach(function (k) {
$location.search(k, $scope.model[k]);
});
});
})
Now you can call createUrlWithCurrentState
with ng-change
of each input
element which has an ng-model
and the state will be saved with each change, or you can call this function in ng-click
on Create a link to this page
button.
You will have to take care to keep allProperties
and defaultValues
updated to save all the required parameters, though.
As to whether this should be done or not, the answer depends on your use case. If you have to allow sharing of links, then there are very few alternatives to keeping state in URL.
However, some state may not be easy to serialize or the data may just be too long to save in the URL.
If you want to preserve the retrieve information only for the current session or browser, you can look at the $cookieStore
or the DOM Storage API.
本文标签: javascriptStore view state in URL using AngularjsStack Overflow
版权声明:本文标题:javascript - Store view state in URL using Angularjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744898186a2631190.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论