angularjs cookies最新版本1.4.1

时间:2015-06-24 11:35:30

标签: javascript angularjs cookies

您好我想在浏览器中存储一个示例cookie并阅读它。这对我不起作用

这是示例html代码:

<!DOCTYPE html>
<html ng-app="KendoDemos">
<head>
   <script src="/home/user/Desktop/kendo/angular.min.js"></script>
   <script src="/home/user/Desktop/kendo/angular-cookies.min.js"></script>
   <script src="/home/user/Desktop/kendo/app.js"></script>
 </head>
 <!-- Body tag augmented with ngController directive  -->
 <body ng-controller="MyCtrl">
    <h1>Hello</h1>

 </body>
</html>

这是我的app.js文件:

 var app = angular.module("KendoDemos", ["ngCookies"]);
 app.controller("MyCtrl",['$scope','$cookies','$cookieStore',MyCtrl]);
 function MyCtrl($scope,$cookies,$cookieStore){
    var favoriteCookie = $cookies.get('myFavorite');
    // Setting a cookie
    $cookies.put('myFavorite', 'oatmeal');
    console.log(favoriteCookie);
 }

请帮帮我。

1 个答案:

答案 0 :(得分:0)

我认为你没有问题是合乎逻辑的,你的cookie myFavorite在设置之前没有设置,所以你必须切换设置和获取的指令。

var app = angular.module("KendoDemos", ["ngCookies"]);
 app.controller("MyCtrl",['$scope','$cookies','$cookieStore',MyCtrl]);
 function MyCtrl($scope,$cookies,$cookieStore){
    // Setting a cookie
    $cookies.put('myFavorite', 'oatmeal');
    var favoriteCookie = $cookies.get('myFavorite');
    console.log(favoriteCookie);
 }