Shane Shane - 1 year ago 87
AngularJS Question

getting values from sessionStorage

I am trying to get the value from sessionStorage and map it to my ng-model, but
when i do that in my ng-init its not working.

The problem is in my actual code i am inside a ng-repeat so my sessionStorage becomes like below:


HTMl Code:

<div ng-app="">
<div ng-controller="MyCtrl">

Not Working <input ng-init = 'name = sessionStorage.getItem("SavedString")' type="text" ng-model="name" >

Working <input type="text" ng-model="name1" >


function MyCtrl($scope) {

sessionStorage.setItem("SavedString","I'm a value saved with SessionStorage");

$ = "test"
$scope.name1 = sessionStorage.getItem("SavedString");

$scope.hi = 'Hello World';


Answer Source

i too tried in your way but i didn't find a the requirements to achieve this i tried with making a function call with in init and appended session data to that model then i can see the proper output by making ng-init ='some()' here is the working plunker


From some source i found that HTML cannot understand session variables directly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download