angular.js - $cookies和$rootScope, 哪一个更适合存储用户数据信息?
过去多啦不再A梦
过去多啦不再A梦 2017-05-15 17:14:24
0
2
735
if (user.username && user.password) {
    loginService.loginUser(user).then(
        function(response) {
            $cookies.put("token", response.token)
            $rootScope.token = response.token
            $cookies.put("username", response.username)
            }
}

token直接放在$rootScope也可以达到存储的目的,所以$cookies有啥用?

过去多啦不再A梦
过去多啦不再A梦

reply all(2)
阿神

My personal understanding is that $rootScope is actually only stored in memory. If the page is refreshed, the contents of $rootScope will be cleared.
Cookies do not have such problems, but the storage capacity of cookies is somewhat limited

PHPzhong

This involves the difference between persistent storage and temporary ($rootScope) storage. In addition, localStorage and sessionStorage are also defined in the HTML5 specification. In addition, if you use the above storage solution, it will involve object serialization and deserialization. The method of using $rootScope does not involve the above content. Finally, if you are interested, please check out the advantages and disadvantages of different storage methods. If you are building a webapp, you can further understand the problems existing in localStorage under low memory. To summarize, choose a storage method based on whether the data needs to be persisted.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template