I have injected $window properly. But when I try to use $window.localStorage, the object localStorage is empty. What I am doing wrong?
Try to use localStorage.setItem , the example: localstorage
If the local storage is empty, it doesn't mean its wrong or you are doing wrong. You can set items if required as follows:
// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");
Related
There's a Location class like this:-
class Location
{
String name;
String value;
}
I have a variable in session like this in my JSP:-
<% EmployeeBean empBean = (EmployeeBean)session.getAttribute("INEmployeeBean");
Location[] locationList = empBean.getLocations(); %>
I need to iterate this 'locationList' in angularJS and populate the value in drop down.
I am accessing locationList in ng-options like this:-
ng-options = "location.value for location in locationList track by location.name"
I need to know how to access this locationList in angularJS, I have tried to set this in ng-init and use but it takes the value as string not array of objects.
Also I have tried setting locationList in backend spring like this and tried to access this in angularJS:-
ModalAndView mv = new ModalAndView('location');
mv.addObject('locationList',locationList);
Please let me know possible approach to make this work.
Thanks,
Ritika
Assuming that the locationList comes in the shape of '["London","Paris","New York"]', that would be an un-parsed JSON, because browser storage works based on Strings, for space efficiency.
To parse it, you can use JSON.parse(locationList);
I've made a JSFiddle showing the string being parsed into a JSON. (Open dev tools and look for console.logs)
In case there's a problem when setting the stringified array in sessionStorage, you could use angular.toJson() to make sure it's a valid format.
Docs here.
Can anybody help me out from how to use $localStorage in angularjs, I can't find a complete guide for it.
I need explanation and syntax for usage such as,
localStorage.getItem();
localStorage.removeItem();
localStorage.clear();
localStorage.setItem();
Providing entire doc would help me well.
Save to localStorage:
localStorage.setItem("name", value);
Get from localStorage
var x = localStorage.getItem("name");
Remove from localStorage:
localStorage.removeItem("name");
Clear localStorage (remove all):
localStorage.clear();
Values in localStorage will be exists when you reopen your browser. When you need clear all when you close browser, you must use sessionStorage. Usage is same as localStorage
Slightly new to AngularJS so please bear with me.
I'm trying to implement ngStorage but had a doubt. If I assign something like this:
$scope.$storage = $localStorage.default({
var: 'foo'
});
And then if $scope.storage.var is changed when an AJAX call is made, does it also change the value of $localStorage.var or do I have to manually re-assign it?
If I do have to manually re-assign it, what's the best way to do so among the options below? (Please do let me know if there's any other way to do it)
$localStorage.var = 'foo2';
$localStorage.$reset({
var : 'foo2'
});
delete $localStorage.var followed by $localStorage.var = 'foo2'
Thanks in advance.
I just used the third option and it seemed to work correctly. That is:
delete $localStorage.var;
$localStorage.var = 'foo2';
So lets say I want to define an var array=['a','b','c']
How do I pass this array into localStorage ?
Should I just simply write $localStorage.data = array;?
And also how to check if that array $localStorage.data is not empty and not undefined?
You can simply store in localstorage as:
localStorage.setItem("data", JSON.stringify(array));
and later retrieve it as
$scope.data= JSON.parse(localStorage.getItem("data"));
You can check $scope.data by doing
console.log($scope.data);
How do I pass this array into localStorage ?
Should I just simply write $localStorage.data = array;?
Yes, you simply put it like this:
$localStorage.data = array
And also how to check if that array $localStorage.data is not empty and not undefined?
Check it like simple variable
if($localStorage.data){
console.log('empty');
} else{
console.log('Data', $localStorage.data);
}
ngStorage
Right from AngularJS homepage: "Unlike other frameworks, there is no need to [...] wrap the model in accessors methods. Just plain old JavaScript here." Now you can enjoy the same benefit while achieving data persistence with Web Storage.
ng-options="branch as branch.name for branch in ordersItemClientInfo.branches track by branch.id"
In my case "branches" is an array of object and each of them have field ref to so deep object (map geoObject). Angular tries to copy thats object and fails!
from angular.js:
getViewValueFromOption: function(option) {
// If the viewValue could be an object that may be mutated by the application,
// we need to make a copy and not return the reference to the value on the option.
return trackBy ? angular.copy(option.viewValue) : option.viewValue;
}
I don't need a copy, I need original object to be selected. How can I make it?
Maybe you can try to do a JSON.stringify (to each item before showing it) and then make the JSON.parse in the template whenever you want to display it?