Session or LocalStorage CRUD table in AngularJS











up vote
0
down vote

favorite












I am creating a CRUD webpage using angularjs and localstorage. The add, edit and delete functions all work perfectly well. I was able to use localstorage and save data after adding or deleting a user.



But i am unable to save edited user details on localstorage. They get erased on refresh of the webpage. The $scope.selectUser is the function I use to edit a user. Thanks in advance.






var myApp = angular.module("myApp", );


myApp.controller("myController", function ($scope) {
console.log("in controller...");
$scope.newUser = {};
$scope.info = "";

if (localStorage.getItem("users") === null) {
$scope.users = [
{ email: "John@yahoo.com", firstName: "John", lastName: "Doe", contact: "281-283-2480", role: "Supplier-Admin", company: "Apple" },
{ email: "Rick@yahoo.com", firstName: "Rick", lastName: "Fraiser", contact: "987-283-2489", role: "Supplier-User", company: "Apple" },
{ email: "Sam@yahoo.com", firstName: "Sam", lastName: "Tarly", contact: "456-786-2480", role: "BuyerAdmin", company: "Samsung" }
];
localStorage.setItem("users", JSON.stringify($scope.users));
} else {
$scope.users = JSON.parse(localStorage.getItem("users"));
}


$scope.saveUser = function () {
console.log("Saving...");
$scope.users.push($scope.newUser);
$scope.info = "New User Added Successfully!";
$scope.newUser = {};
localStorage.setItem("users", JSON.stringify($scope.users));
//localStorage.setItem("email", $scope.users);
};

$scope.selectUser = function (user) {
$scope.clickedUser = user;
localStorage.setItem("users", JSON.stringify($scope.users));
};

$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
localStorage.setItem("users", JSON.stringify($scope.users));
$scope.info = "User Deleted Successfully!";

};

$scope.clearInfo = function () {
$scope.info = "";
};
});












share|improve this question
























  • local and session storage provide fairly simple API's (getItem, setItem, etc). What specifically do you need assistance with?
    – Frank Modica
    Nov 9 at 18:08












  • I don't see where you've tried to implement session or local storage. However, I would highly suggest using a library for this. One I've used I'll link here
    – Mickers
    Nov 9 at 19:14










  • @FrankModica When I add, delete or edit a user, they should not be erased when I refresh the web page or move to another page and coming back again. To do that, I need to use the localstorage or sessionstorage. I just don't know where and how to implement them.
    – Vaibhav Shankar
    Nov 9 at 19:53










  • @Mickers Yes. I haven't implemented session or local storage as I don't know where and how to write them. I'm looking for some guidance.
    – Vaibhav Shankar
    Nov 9 at 19:55






  • 1




    @VaibhavShankar They're part of the global object. You just have to do things like localStorage.setItem('users', JSON.stringify($scope.users)) and then the opposite to get them out of storage, like JSON.parse(localStorage.getItem('users'))
    – Frank Modica
    Nov 9 at 20:00

















up vote
0
down vote

favorite












I am creating a CRUD webpage using angularjs and localstorage. The add, edit and delete functions all work perfectly well. I was able to use localstorage and save data after adding or deleting a user.



But i am unable to save edited user details on localstorage. They get erased on refresh of the webpage. The $scope.selectUser is the function I use to edit a user. Thanks in advance.






var myApp = angular.module("myApp", );


myApp.controller("myController", function ($scope) {
console.log("in controller...");
$scope.newUser = {};
$scope.info = "";

if (localStorage.getItem("users") === null) {
$scope.users = [
{ email: "John@yahoo.com", firstName: "John", lastName: "Doe", contact: "281-283-2480", role: "Supplier-Admin", company: "Apple" },
{ email: "Rick@yahoo.com", firstName: "Rick", lastName: "Fraiser", contact: "987-283-2489", role: "Supplier-User", company: "Apple" },
{ email: "Sam@yahoo.com", firstName: "Sam", lastName: "Tarly", contact: "456-786-2480", role: "BuyerAdmin", company: "Samsung" }
];
localStorage.setItem("users", JSON.stringify($scope.users));
} else {
$scope.users = JSON.parse(localStorage.getItem("users"));
}


$scope.saveUser = function () {
console.log("Saving...");
$scope.users.push($scope.newUser);
$scope.info = "New User Added Successfully!";
$scope.newUser = {};
localStorage.setItem("users", JSON.stringify($scope.users));
//localStorage.setItem("email", $scope.users);
};

$scope.selectUser = function (user) {
$scope.clickedUser = user;
localStorage.setItem("users", JSON.stringify($scope.users));
};

$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
localStorage.setItem("users", JSON.stringify($scope.users));
$scope.info = "User Deleted Successfully!";

};

$scope.clearInfo = function () {
$scope.info = "";
};
});












share|improve this question
























  • local and session storage provide fairly simple API's (getItem, setItem, etc). What specifically do you need assistance with?
    – Frank Modica
    Nov 9 at 18:08












  • I don't see where you've tried to implement session or local storage. However, I would highly suggest using a library for this. One I've used I'll link here
    – Mickers
    Nov 9 at 19:14










  • @FrankModica When I add, delete or edit a user, they should not be erased when I refresh the web page or move to another page and coming back again. To do that, I need to use the localstorage or sessionstorage. I just don't know where and how to implement them.
    – Vaibhav Shankar
    Nov 9 at 19:53










  • @Mickers Yes. I haven't implemented session or local storage as I don't know where and how to write them. I'm looking for some guidance.
    – Vaibhav Shankar
    Nov 9 at 19:55






  • 1




    @VaibhavShankar They're part of the global object. You just have to do things like localStorage.setItem('users', JSON.stringify($scope.users)) and then the opposite to get them out of storage, like JSON.parse(localStorage.getItem('users'))
    – Frank Modica
    Nov 9 at 20:00















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am creating a CRUD webpage using angularjs and localstorage. The add, edit and delete functions all work perfectly well. I was able to use localstorage and save data after adding or deleting a user.



But i am unable to save edited user details on localstorage. They get erased on refresh of the webpage. The $scope.selectUser is the function I use to edit a user. Thanks in advance.






var myApp = angular.module("myApp", );


myApp.controller("myController", function ($scope) {
console.log("in controller...");
$scope.newUser = {};
$scope.info = "";

if (localStorage.getItem("users") === null) {
$scope.users = [
{ email: "John@yahoo.com", firstName: "John", lastName: "Doe", contact: "281-283-2480", role: "Supplier-Admin", company: "Apple" },
{ email: "Rick@yahoo.com", firstName: "Rick", lastName: "Fraiser", contact: "987-283-2489", role: "Supplier-User", company: "Apple" },
{ email: "Sam@yahoo.com", firstName: "Sam", lastName: "Tarly", contact: "456-786-2480", role: "BuyerAdmin", company: "Samsung" }
];
localStorage.setItem("users", JSON.stringify($scope.users));
} else {
$scope.users = JSON.parse(localStorage.getItem("users"));
}


$scope.saveUser = function () {
console.log("Saving...");
$scope.users.push($scope.newUser);
$scope.info = "New User Added Successfully!";
$scope.newUser = {};
localStorage.setItem("users", JSON.stringify($scope.users));
//localStorage.setItem("email", $scope.users);
};

$scope.selectUser = function (user) {
$scope.clickedUser = user;
localStorage.setItem("users", JSON.stringify($scope.users));
};

$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
localStorage.setItem("users", JSON.stringify($scope.users));
$scope.info = "User Deleted Successfully!";

};

$scope.clearInfo = function () {
$scope.info = "";
};
});












share|improve this question















I am creating a CRUD webpage using angularjs and localstorage. The add, edit and delete functions all work perfectly well. I was able to use localstorage and save data after adding or deleting a user.



But i am unable to save edited user details on localstorage. They get erased on refresh of the webpage. The $scope.selectUser is the function I use to edit a user. Thanks in advance.






var myApp = angular.module("myApp", );


myApp.controller("myController", function ($scope) {
console.log("in controller...");
$scope.newUser = {};
$scope.info = "";

if (localStorage.getItem("users") === null) {
$scope.users = [
{ email: "John@yahoo.com", firstName: "John", lastName: "Doe", contact: "281-283-2480", role: "Supplier-Admin", company: "Apple" },
{ email: "Rick@yahoo.com", firstName: "Rick", lastName: "Fraiser", contact: "987-283-2489", role: "Supplier-User", company: "Apple" },
{ email: "Sam@yahoo.com", firstName: "Sam", lastName: "Tarly", contact: "456-786-2480", role: "BuyerAdmin", company: "Samsung" }
];
localStorage.setItem("users", JSON.stringify($scope.users));
} else {
$scope.users = JSON.parse(localStorage.getItem("users"));
}


$scope.saveUser = function () {
console.log("Saving...");
$scope.users.push($scope.newUser);
$scope.info = "New User Added Successfully!";
$scope.newUser = {};
localStorage.setItem("users", JSON.stringify($scope.users));
//localStorage.setItem("email", $scope.users);
};

$scope.selectUser = function (user) {
$scope.clickedUser = user;
localStorage.setItem("users", JSON.stringify($scope.users));
};

$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
localStorage.setItem("users", JSON.stringify($scope.users));
$scope.info = "User Deleted Successfully!";

};

$scope.clearInfo = function () {
$scope.info = "";
};
});








var myApp = angular.module("myApp", );


myApp.controller("myController", function ($scope) {
console.log("in controller...");
$scope.newUser = {};
$scope.info = "";

if (localStorage.getItem("users") === null) {
$scope.users = [
{ email: "John@yahoo.com", firstName: "John", lastName: "Doe", contact: "281-283-2480", role: "Supplier-Admin", company: "Apple" },
{ email: "Rick@yahoo.com", firstName: "Rick", lastName: "Fraiser", contact: "987-283-2489", role: "Supplier-User", company: "Apple" },
{ email: "Sam@yahoo.com", firstName: "Sam", lastName: "Tarly", contact: "456-786-2480", role: "BuyerAdmin", company: "Samsung" }
];
localStorage.setItem("users", JSON.stringify($scope.users));
} else {
$scope.users = JSON.parse(localStorage.getItem("users"));
}


$scope.saveUser = function () {
console.log("Saving...");
$scope.users.push($scope.newUser);
$scope.info = "New User Added Successfully!";
$scope.newUser = {};
localStorage.setItem("users", JSON.stringify($scope.users));
//localStorage.setItem("email", $scope.users);
};

$scope.selectUser = function (user) {
$scope.clickedUser = user;
localStorage.setItem("users", JSON.stringify($scope.users));
};

$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
localStorage.setItem("users", JSON.stringify($scope.users));
$scope.info = "User Deleted Successfully!";

};

$scope.clearInfo = function () {
$scope.info = "";
};
});





var myApp = angular.module("myApp", );


myApp.controller("myController", function ($scope) {
console.log("in controller...");
$scope.newUser = {};
$scope.info = "";

if (localStorage.getItem("users") === null) {
$scope.users = [
{ email: "John@yahoo.com", firstName: "John", lastName: "Doe", contact: "281-283-2480", role: "Supplier-Admin", company: "Apple" },
{ email: "Rick@yahoo.com", firstName: "Rick", lastName: "Fraiser", contact: "987-283-2489", role: "Supplier-User", company: "Apple" },
{ email: "Sam@yahoo.com", firstName: "Sam", lastName: "Tarly", contact: "456-786-2480", role: "BuyerAdmin", company: "Samsung" }
];
localStorage.setItem("users", JSON.stringify($scope.users));
} else {
$scope.users = JSON.parse(localStorage.getItem("users"));
}


$scope.saveUser = function () {
console.log("Saving...");
$scope.users.push($scope.newUser);
$scope.info = "New User Added Successfully!";
$scope.newUser = {};
localStorage.setItem("users", JSON.stringify($scope.users));
//localStorage.setItem("email", $scope.users);
};

$scope.selectUser = function (user) {
$scope.clickedUser = user;
localStorage.setItem("users", JSON.stringify($scope.users));
};

$scope.deleteUser = function () {
console.log($scope.users.indexOf($scope.clickedUser));
$scope.users.splice($scope.users.indexOf($scope.clickedUser), 1);
localStorage.setItem("users", JSON.stringify($scope.users));
$scope.info = "User Deleted Successfully!";

};

$scope.clearInfo = function () {
$scope.info = "";
};
});






javascript html angularjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 2:04

























asked Nov 9 at 18:03









Vaibhav Shankar

187




187












  • local and session storage provide fairly simple API's (getItem, setItem, etc). What specifically do you need assistance with?
    – Frank Modica
    Nov 9 at 18:08












  • I don't see where you've tried to implement session or local storage. However, I would highly suggest using a library for this. One I've used I'll link here
    – Mickers
    Nov 9 at 19:14










  • @FrankModica When I add, delete or edit a user, they should not be erased when I refresh the web page or move to another page and coming back again. To do that, I need to use the localstorage or sessionstorage. I just don't know where and how to implement them.
    – Vaibhav Shankar
    Nov 9 at 19:53










  • @Mickers Yes. I haven't implemented session or local storage as I don't know where and how to write them. I'm looking for some guidance.
    – Vaibhav Shankar
    Nov 9 at 19:55






  • 1




    @VaibhavShankar They're part of the global object. You just have to do things like localStorage.setItem('users', JSON.stringify($scope.users)) and then the opposite to get them out of storage, like JSON.parse(localStorage.getItem('users'))
    – Frank Modica
    Nov 9 at 20:00




















  • local and session storage provide fairly simple API's (getItem, setItem, etc). What specifically do you need assistance with?
    – Frank Modica
    Nov 9 at 18:08












  • I don't see where you've tried to implement session or local storage. However, I would highly suggest using a library for this. One I've used I'll link here
    – Mickers
    Nov 9 at 19:14










  • @FrankModica When I add, delete or edit a user, they should not be erased when I refresh the web page or move to another page and coming back again. To do that, I need to use the localstorage or sessionstorage. I just don't know where and how to implement them.
    – Vaibhav Shankar
    Nov 9 at 19:53










  • @Mickers Yes. I haven't implemented session or local storage as I don't know where and how to write them. I'm looking for some guidance.
    – Vaibhav Shankar
    Nov 9 at 19:55






  • 1




    @VaibhavShankar They're part of the global object. You just have to do things like localStorage.setItem('users', JSON.stringify($scope.users)) and then the opposite to get them out of storage, like JSON.parse(localStorage.getItem('users'))
    – Frank Modica
    Nov 9 at 20:00


















local and session storage provide fairly simple API's (getItem, setItem, etc). What specifically do you need assistance with?
– Frank Modica
Nov 9 at 18:08






local and session storage provide fairly simple API's (getItem, setItem, etc). What specifically do you need assistance with?
– Frank Modica
Nov 9 at 18:08














I don't see where you've tried to implement session or local storage. However, I would highly suggest using a library for this. One I've used I'll link here
– Mickers
Nov 9 at 19:14




I don't see where you've tried to implement session or local storage. However, I would highly suggest using a library for this. One I've used I'll link here
– Mickers
Nov 9 at 19:14












@FrankModica When I add, delete or edit a user, they should not be erased when I refresh the web page or move to another page and coming back again. To do that, I need to use the localstorage or sessionstorage. I just don't know where and how to implement them.
– Vaibhav Shankar
Nov 9 at 19:53




@FrankModica When I add, delete or edit a user, they should not be erased when I refresh the web page or move to another page and coming back again. To do that, I need to use the localstorage or sessionstorage. I just don't know where and how to implement them.
– Vaibhav Shankar
Nov 9 at 19:53












@Mickers Yes. I haven't implemented session or local storage as I don't know where and how to write them. I'm looking for some guidance.
– Vaibhav Shankar
Nov 9 at 19:55




@Mickers Yes. I haven't implemented session or local storage as I don't know where and how to write them. I'm looking for some guidance.
– Vaibhav Shankar
Nov 9 at 19:55




1




1




@VaibhavShankar They're part of the global object. You just have to do things like localStorage.setItem('users', JSON.stringify($scope.users)) and then the opposite to get them out of storage, like JSON.parse(localStorage.getItem('users'))
– Frank Modica
Nov 9 at 20:00






@VaibhavShankar They're part of the global object. You just have to do things like localStorage.setItem('users', JSON.stringify($scope.users)) and then the opposite to get them out of storage, like JSON.parse(localStorage.getItem('users'))
– Frank Modica
Nov 9 at 20:00



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231123%2fsession-or-localstorage-crud-table-in-angularjs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231123%2fsession-or-localstorage-crud-table-in-angularjs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()