如何使用angularjs操作cookie

魔豆發表於2016-01-31
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html ng-app="myApp">
<head>
<title>angularjs-cookie</title>
<script type="text/javascript" src="js/angular/angular.min.js"></script>
<script type="text/javascript" src="js/angular/angular-cookies.min.js"></script>
</head>
<body>
<form name="form1" ng-controller="form1">
    <input type="text" name="input1" value="" ng-model="input1" />
    <input type="button" value="儲存cookie" ng-click="saveCookie()" />
    <input type="button" value="刪除cookie" ng-click="removeCookie()" />
<script>
    var app = angular.module('myApp',['ngCookies']);
    app.controller("form1",function($cookieStore,$scope){
        if($cookieStore.get("a1")) {
            $scope.input1 = $cookieStore.get("a1");
            alert("載入cookie成功!");
        }
        $scope.saveCookie = function() {
            $cookieStore.put("a1",$scope.input1);
            alert("儲存cookie成功!");
        };
        $scope.removeCookie = function() {
            $cookieStore.remove("a1");
            alert("刪除cookie成功!");
        };
    });
    </script>
    </form>
</body>
</html>

 

相關文章