How to pass a simple array data in ngStorage,$localstorage? - angularjs

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.

Related

Nested Loop inside reactjs

I am new to React JS & currently trying to iterate a certain data to present the same in react js but not able to do the same. The data which looks something like this
Now, the final output should be look something like this in tabular format
The things which I tried are:-
The error which I am getting below one is for 1 image and for second, the code is not getting parsed.
[![error][5]][5]
So, how to achieve the desired output in react js
It looks like you're trying to use map onto an object, while you should use it on a collection. Maybe try something like this :
Object.values(listData.data).map((meetingRoom) => { // your code here });
This will allow you to use the content inside your data object as an array of objects.
Edit : Sorry, I didn't understand you need to access the key as well as the value. To achieve that you can simply use Object.entries which will return the key (Meeting Room 1, Meeting Room 2 in this instance) in the first variable and the array of items in the second variable.
Here's a quick example:
Object.entries(listData.data).forEach(([key, value]) => {
console.log(key, value);
// You could use value.map() to iterate over each object in your meeting room field array.
});
Note : you can also use a for (... of ...) loop like this instead of a forEach :
for (const [key, value] of Object.entries(listData.data)) {
console.log(key, value);
};
For more information about the Object.entries method, feel free to check the MDN Webdocs page about it here.

Why my $window.localStorage object is empty?

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

How to access session variable which is array of object in angular js

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.

MongoDB returns array of "m" objects?

I have setup backend with NodeJS and MongoDB. In frontend, I am using AngularJS and ngResource. When I run this block of code:
$scope.users = User.query(function() {
console.log($scope.users);
});
it returns this:
What those 'm' letters mean? All of those are objects including the right data, so it works, I was just thinking what does this mean.
It's the name of the constructor that created the object. You can also see this below those values with Promise and Array.

Why array in $scope doesn't updated using array concat method?

When I use concat method, my $scope is not updated
var anotherList = ["2", "3", "4"];
$scope.list = [];
$scope.list.concat(anotherList);
But using array push method in a loop, my $scope gets updated
Your syntax is wrong. You need to assign the return value of .concat() to your scoped array.
$scope.list = $scope.list.concat(anotherList);
Alternatively, if you used call() you could bind the result directly to the scoped array without the need to "reassign" since it would be done under the hood for you.
$scope.list.concat.call(anotherList);
See the documentation.
What David said is correct. Just to add: concat() doesn't modify the array, it just return a new array based on the parameters received. Check this for more info:
http://www.w3schools.com/jsref/jsref_concat_string.asp
If you want to maintain the original array, instead of concat you can do:
Array.prototype.push.apply($scope.list, anotherList);

Resources