ElenaDBA ElenaDBA - 3 months ago 5
AngularJS Question

What is the best way to store key value pairs in Angular JS

I have an angular app that consists of a main page and partial pages. Partial pages are displayed in


<div class="" ui-view>


To store a global variable I use
. So I declared the variable in the base controller:

$scope.$parent.my_array = [];

and attempted to fill it from the partial page controller using:

$scope.$parent.my_array.push({ 'KeyId': KeyVariableID, 'KeyValue': KeyValue });

but that does not seem to do anything. Can anyone help?


After doing some research I ended up using the following service:

.service('MyService', function() {
  // Create a reference to ourself
  var sv = this;

  // Initialise the data structure
  sv.data = { };

  // Function to set an item in the map
  sv.setItem = function(key, value) {
    sv.data[key] = value;

  // Function to retrieve a value from the map
  sv.getItem = function(key) {
    if (sv.data[key] != undefined) {
      return sv.data[key];
    return false;