Angular Cookie 读写

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Angular Cookie 读写

飞翔的小仓鼠   2022-05-23 我要评论

Angular Cookie 读写操作,代码如下所示:

var app = angular.module('Mywind',['ui.router'])
app.controller('Myautumn',function($scope,$http,$filter){
//angular Cookie写
//用法 :$scioe.addCookie("姓名",值,时间,"/")
$scope.addCookie = function(name, value, days, path) {
  var name = decodeURI(name);
  var value = decodeURI(value);
  var expires = new Date();
  expires.setTime(expires.getTime() + days * 3600000 * 24);
  path = path == "" ? "" : ";path=" + path;
  var _expires = (typeof days) == "string" ? "" : ";expires=" + expires.toUTCString();
  document.cookie = name + "=" + value + _expires + path;
}
//angular Cookie读
//用法:$scope.getCookieValue("姓名")
  $scope.getCookieValue = function(name) {
    var name = decodeURI(name);
    var allcookies = document.cookie;
    name += "=";
    var pos = allcookies.indexOf(name);
    if(pos != -1) {
      var start = pos + name.length;
      var end = allcookies.indexOf(";", start);
    if(end == -1) end = allcookies.length;
      var value = allcookies.substring(start, end);
      return(value);
    } else {
      return "";
    }
  };
}) 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们