Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJs$cookies不能与$window.location.href一起使用_Angularjs_Angular Cookies - Fatal编程技术网

AngularJs$cookies不能与$window.location.href一起使用

AngularJs$cookies不能与$window.location.href一起使用,angularjs,angular-cookies,Angularjs,Angular Cookies,嗨,我正在学习AngularJS,并尝试保存cookies值 我有两个html文件 index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="angular.min.js"></script> <script

嗨,我正在学习AngularJS,并尝试保存cookies值 我有两个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>
我在Cookie中添加了一些文本,并重定向到test.html以显示Cookie值,但它在什么地方丢失了


是否存在$window.location.href重置cookies值?

已解决!,刚刚忘记添加ng init=“init()”已解决!,只是忘记了添加ng init=“init()”
<!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>
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');
        }
    });