irishstorm

Untitled

Aug 19th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. angular.module("app", []).controller("MyController", [ '$scope', '$http', function($scope, $http) {
  2. // This function populates the localstorage (if empty) and creates the characters-array
  3. var init = function () {
  4. var storageChars = localStorage.getItem('characters');
  5.  
  6. if (!!storageChars) {
  7. $scope.characters = JSON.parse(storageChars);
  8. } else {
  9. $scope.characters = [ { name: 'irishstorm', level: 60, server: 'The Red Eclipse' } ];
  10. }
  11. };
  12.  
  13. init.call();
  14.  
  15. $scope.addCharacter = function() {
  16. $scope.characters.push({
  17. name: $scope.addName,
  18. level: $scope.addLevel,
  19. server: $scope.addServer
  20. });
  21.  
  22. $scope.commitChanges();
  23. };
  24.  
  25. $scope.deleteCharacter = function(index) {
  26. $scope.characters.splice(index, 1); // Removes from the array.
  27.  
  28. $scope.commitChanges();
  29. };
  30.  
  31. // This function commits the changes of the characters-array to the localStorage.
  32. $scope.commitChanges = function () {
  33. localStorage.setItem('characters', JSON.stringify($scope.characters));
  34. };
  35. }]);
Advertisement
Add Comment
Please, Sign In to add comment