1

こんにちは、私はAngularJSを学んでいて、クッキーの値を保存しようとしています.2つのhtmlファイルがあります

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="angular.min.js"></script>
    <script src="angular-cookies.min.js"></script>
    <script src="angular-route.min.js"></script>
    <script src="test.js"></script>
</head>
<body ng-app="myApp" ng-controller="helloCtrl">
    Hello <input type="text" ng-model="helloText"/>
    <input type="button" ng-click = "redirectToTest()" value="Go" />
</body>
</html>

test.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="angular.min.js"></script>
    <script src="angular-cookies.min.js"></script>
    <script src="test.js"></script>
</head>
<body ng-app="myApp" ng-controller="testCtrl">
    Hello {{cookiesText}}
</body>
</html>

test.js

var myApp = angular.module('myApp', ['ngCookies']);

var helloCtrl = myApp.controller('helloCtrl',
    function ($scope, $cookies, $window) {

        $scope.helloText = "Hello World!"

        $scope.redirectToTest = function(){
            $cookies.put('hello', $scope.helloText);
            console.log("Added hello to cookies : "+ $cookies.get('hello'));
            $window.location.href = "test.html";
        }
    });

var testCtrl = myApp.controller('testCtrl',
    function ($scope,$cookies) {
        $scope.cookiesText = '';
        $scope.init= function(){
            $scope.cookiesText = $cookies.get('hello');
        }
    });

Cookie にテキストを追加し、test.html にリダイレクトして Cookie の値を表示していますが、どこかで失われましたか?

$window.location.href リセット Cookie 値はありますか?

4

0 に答える 0