sessionStorage不起作用

时间:2016-07-27 08:45:13

标签: angularjs

代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('MyApp', ["$window"]);
        app.controller('MyController', function ($scope, $window) {
            $scope.Save = function () {
                $window.sessionStorage.setItem("app1", "SessionStorage: My name is Mudassar Khan.");
            }
            $scope.Get = function () {
                $window.alert("OK");
                $window.alert($window.sessionStorage.getItem("app1"));
            }
        });
    </script>
</head>
<body>
    <div ng-app="MyApp" ng-controller="MyController">
        <input type="button" value="Save" ng-click="Save()" />
        <input type="button" value="Get" ng-click="Get()" />
    </div>
</body>
</html>

没有发生任何事情。

2 个答案:

答案 0 :(得分:1)

窗口依赖性的角度检查失败。因此,请将其从module中删除,并仅包含在controller

var app = angular.module('MyApp', []);

答案 1 :(得分:0)

声明模块时,您已将$window作为依赖项传递给它。 Angular将其视为外部模块并尝试找到它,然后您会遇到此异常:

Module '$window' is not available!

$window不是模块,它是window对象的角度包装器。

试试这个:

var app = angular.module('MyApp', []);