在页面刷新时保留来自 cookie 存储的范围值

Retaining scope value from the cookie store on page refresh

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

$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

我认为您需要对 $cookie 使用 .get 和 .put 而不是常规的点表示法。您可以在 1.4 version docs 中看到 - 假设这就是您正在使用的内容。