简体   繁体   中英

Error: [$injector:unpr] in sessionStorage

I am trying to store values from a form using sessionStorage. But it is throwing error,

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

this is my app.js file

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);
 };
}]);

And more doubt also, can anyone give me a simple example to do routing in angular js using $UrlRouterProvider. I want to get values from a form(reg.html) using sessionStorage and need to route to an another page(sum.html) and echo out the values from form(reg.html) there. The above code have lot of errors, any help would be life saving.. Thanks in advance.

Firstly along with the AngularJS JavaScript file, ngStorage.min.js file also needs to be inherited in order to perform operations on HTML5 LocalStorage and SessionStorage objects. Then you need to pass ngStorage parameter in angular.module definition and then inject the $localStorage, $sessionStorage dependency in the controller.

Here is an example on how to save and get the data to and from the sessionStorage. Here i have two functions

  1. to save data to sessionStorage
  2. to get data

     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); } }); 

    I hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM