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有啥用?
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
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.