在页面刷新时保留 Cookie 存储区的范围值

Retaining scope value from the cookie store on page refresh

本文关键字:存储区 范围 Cookie 保留 刷新      更新时间:2023-09-26

我的一个控制器中有以下代码。

$scope.artistId = $cookies.artistId; 
$scope.artistName = $cookies.artistName;
$scope.switchArtist = function(artist) {
    $scope.artistName = '';
    $scope.artistId = '';
    $scope.artist = artist;
    $cookies.artistName = artist.name;
    $cookies.artistId = artist.id;
    $scope.artistName = artist.name;
    $scope.artistId = artist.id;
    $rootScope.$broadcast(ARTIST_EVENTS.switchedArtist);
};

那么在我看来,我有

{{ artistName }}

当我触发 $scope.switchArtist(); 函数时,它起作用了。但是,当我刷新页面时,$scope.artistName 再次在我的视图中失去它的价值。

[编辑]

我正在使用 angularjs 1.3.14

我相信你需要使用 .get 和 .put 来表示$cookies而不是常规的点符号。 您可以在 1.4 版本文档中看到此处 - 假设这就是您正在使用的。