Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- angular.module("app", []).controller("MyController", [ '$scope', '$http', function($scope, $http) {
- // This function populates the localstorage (if empty) and creates the characters-array
- var init = function () {
- var storageChars = localStorage.getItem('characters');
- if (!!storageChars) {
- $scope.characters = JSON.parse(storageChars);
- } else {
- $scope.characters = [ { name: 'irishstorm', level: 60, server: 'The Red Eclipse' } ];
- }
- };
- init.call();
- $scope.addCharacter = function() {
- $scope.characters.push({
- name: $scope.addName,
- level: $scope.addLevel,
- server: $scope.addServer
- });
- $scope.commitChanges();
- };
- $scope.deleteCharacter = function(index) {
- $scope.characters.splice(index, 1); // Removes from the array.
- $scope.commitChanges();
- };
- // This function commits the changes of the characters-array to the localStorage.
- $scope.commitChanges = function () {
- localStorage.setItem('characters', JSON.stringify($scope.characters));
- };
- }]);
Advertisement
Add Comment
Please, Sign In to add comment