Shane Shane - 10 days ago 6
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:

sessionStorage.getItem(item.itemId)


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" >
</div>
</div>


Controller:

function MyCtrl($scope) {

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

//RETRIEVE VALUE
$scope.name = "test"
$scope.name1 = sessionStorage.getItem("SavedString");


$scope.hi = 'Hello World';
}


Fiddle:
http://jsfiddle.net/393revrn/

Answer

i too tried in your way but i didn't find a the requirements to achieve this ......so 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

UPDATE

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