【问题标题】:Error: [$injector:unpr] in sessionStorage错误:sessionStorage 中的 [$injector:unpr]
【发布时间】:2017-03-16 05:06:37
【问题描述】:

我正在尝试使用 sessionStorage 存储表单中的值。但它会抛出错误,

错误:[$injector:unpr] http://errors.angularjs.org/1.2.25/$injector/unpr?p0=%24sessionStorageProvider%20%3C-%20%24sessionStorage

这是我的 app.js 文件

var BusRegModule = angular.module('BusRegModule', ['ui.router']);

BusRegModule.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
   $urlRouterProvider.otherwise('/');
    /* should route to sum.html(below code was used for checking) */
   $stateProvider.state('home', {      
           url: '/home',
           templateUrl: 'home.html'
   }); 
   $locationProvider.html5Mode(true);      
});

BusRegModule.controller('BusRegController',["$scope","$sessionStorage","$http", function($scope,$sessionStorage,$http){
 $scope.storage=$sessionStorage;
 $scope.businessReg=  {}
 $scope.submitMyForm=function() {        
     var q;
     sessionStorage.setItem("q",JSON.stringify($scope.businessReg));
     var d=sessionStorage.getItem("q"); 
     alert(d);
 };
}]);

还有更多疑问,谁能给我一个简单的例子来使用 $UrlRouterProvider 在 Angular js 中进行路由。我想使用 sessionStorage 从表单(reg.html)中获取值,并且需要路由到另一个页面(sum.html)并在那里回显表单(reg.html)中的值。 上面的代码有很多错误,任何帮助都会挽救生命.. 提前致谢。

【问题讨论】:

  • sessionStorage 全球可用。不需要注入,也不能注入
  • 是sessionStorage你的服务吗?
  • 不,sessionStorage 用于从表单中获取值并存储它。

标签: javascript angularjs


【解决方案1】:

首先与AngularJS JavaScript 文件一起,ngStorage.min.js 文件也需要被继承,以便对HTML5 LocalStorage 和SessionStorage 对象执行操作。 然后需要在 angular.module 定义中传递 ngStorage 参数,然后在控制器中注入 $localStorage, $sessionStorage 依赖。

这是一个关于如何在 sessionStorage 中保存和获取数据的示例。这里我有两个功能

  1. 将数据保存到 sessionStorage
  2. 获取数据

    var app = angular.module('MyApp', ["ngStorage"])
    app.controller('MyController', function ($scope, $localStorage, $sessionStorage, $window) {
        $scope.Save = function () {
            $sessionStorage.SessionMessage = "SessionStorage: Storing some data.";
        }
        $scope.Get = function () {
            $window.alert($sessionStorage.SessionMessage);
        }
    });
    

    我希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    相关资源
    最近更新 更多