I am having trouble in store and read sessioned data with Angularjs
.
After click on button emp_name should store in session
and also how can i read stored emp_name from session
.
Sample in plnkr
// Code goes here
var app = angular.module('app', []);
app.controller('Ctrl', function($scope) {
$scope.employee = [{
emp_id: 1,
emp_name: 'Jes',
emp_cont:9876543445
}, {
emp_id: 2,
emp_name: 'Sandy',
emp_cont:3553454345
}, {
emp_id: 3,
emp_name: 'Alex',
emp_cont:9343434345
}, {
emp_id: 4,
emp_name: 'Nancy',
emp_cont:9876543445
}, {
emp_id: 5,
emp_name: 'Scott',
emp_cont:9834564455
}
];
$scope.returnRefId = function (emp) {
try {
// test emp
alert(emp);
// session code here
}
catch (e) {
alert("some errror");
}
};
});
ul li{list-style:none;float:left;padding:10px;border:1px solid #ddd;height:20px;width:100px}
ul{clear:both}
label{color:red}
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-app="app" ng-controller="Ctrl">
<ul ng-repeat="employees in employee | filter:customFilter">
<li>{{employees.emp_id}} </li>
<li>{{employees.emp_name}} </li>
<li>{{employees.emp_cont}} </li>
<li><button ng-click="returnRefId(employees.emp_name);" >Add Session</button></li>
</ul>
<br /><br /><br />
<label id="read_ses">How to store and read emp_name thru only session</label>
</body>
</html>
I have created alert with emp_name on click by using angularjs
. I want that on button click emp_name should store in session
and for session
testing purpose read sessioned emp_name data somewhere on page.
Any and all help/advice is sincerely appreciated. Thanks.
Your session storage code could look something like this using Javascript APIs for session storage. You will have to serialize your Javascript object as session storage only supports strings.
$scope.returnRefId = function (emp) {
try {
// test emp
alert(emp);
// session code here
sessionStorage.setItem("emp-key", JSON.stringify(emp));
}
catch (e) {
alert("some errror");
}
};
Alternatively, you can storage each property of emp
in a separate storage key. Debugging should be as simple as
sessionStorage.getItem("emp-key")
More information on session storage can be found here. https://developer.mozilla.org/en/docs/Web/API/Window/sessionStorage
Also checkout ngStorage https://github.com/gsklee/ngStorage
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With