JMStudios.jrichardson
JMStudios.jrichardson

Reputation: 416

Angularjs Passing array between controllers

I have been through several tutorials and posts about this topic and still can't seem to figure out what is wrong with my code. To me it seems I am having scoping issues with the data within my service. My code is split up into separate files. Here is my code: github link : https://github.com/StudentJoeyJMStudios/PetPinterest.git

 //in dataService.js
 var app = angular.module('se165PetPinterestWebApp');

 app.service('SharedData', function ()
 {
    var categoryOfSelectedAnimals = [];

    this.setCatOfSelAnimals = function(pulledCategoriesFromParse)
    {

        categoryOfSelectedAnimals = pulledCategoriesFromParse;
        console.log('after assignment in set::' + categoryOfSelectedAnimals);
    };
    this.getCatOfSelAnimals = function()
    {
        console.log('in get::::' + categoryOfSelectedAnimals);

        return categoryOfSelectedAnimals;
    };
 });

in my first controller to set the data in signup.js

app.controller('SignupCtrl',['$scope', 'SharedData', function ($scope, SharedData)
{  
     var Categories = Parse.Object.extend('Categories');
  var query = new Parse.Query(Categories);

  query.find({
      success: function(results)
      {

          $scope.availableCategoriesOfAnimals = results;

          SharedData.setCatOfSelAnimals(results);


      },
      error: function(error)
      {
          alert('Error: ' + error.code + ' ' + error.message);
      }
  });
  };
}]);

Then in my other controller trying to get the data from the array within my service:

var app = angular.module('se165PetPinterestWebApp');
app.controller('CatSelCtrl', function ($scope, SharedData)
{
      $scope.availableCategoriesOfAnimals = SharedData.getCatOfSelAnimals();
});

When I print the contents from the SharedData.getCatOfSelAnimals I get 0 every time. Please help. Thank you very much in advance.

EDIT: After playing around with a string a bit I am finding the changed string in the set function is not saved into the service and when I call my get function within my service the string is not changed from the set method. Please help, thank you in advance.

EDIT: So it looks like when I navigate to new page by using window.location.href = '../views/categorySelection.html'; in my signup.js it reloads my dataService.js which re-sets my variables back to nothing. Does anyone have any ideas as to how to fix this?

Upvotes: 1

Views: 2792

Answers (2)

Edit

First: why you lose data

You need to setup routing properly. Right now you are not changing views but rather using window.location.href to load a new bootstrap file (dashboard.html), i.e. everything saved in memory will be lost. So you have been doing it right, sort of, but the moment you change to dashboard.html all data from Parse is lost.

You can solve this by configuring routes and use $location.url() to change URL. Read more about angular.route here: https://docs.angularjs.org/api/ngRoute/service/$route

The angular way

After looking at your code and running your app I think we need to take a step back. Angular is tricky to get used to but there is a lot of good tutorials. I think you might wanna read some of them to get a better grasp of how it works and how to setup and build your app.

Start here: http://www.airpair.com/angularjs

Boilerplate

Found this boilerplate for an Angular app using Parse. It might be something you could use. https://github.com/brandid/parse-angular-demo


Original

Or an even quicker way to empty $scope.availableCategoriesOfAnimals and then merge new data without breaking reference:

$scope.availableCategoriesOfAnimals.length = 0;
Array.prototype.push.apply($scope.availableCategoriesOfAnimals, pulledCategoriesFromParse);

Upvotes: 2

Sander Elias
Sander Elias

Reputation: 754

You are breaking the reference on assignment. This is a JavaScript issue, not an angular one for that matter ;)

Try this in your set function:

 categoryOfSelectedAnimals.length=0;
 pulledCategoriesFromParse.forEach(function (e) {categoryOfSelectedAnimals.push(e)});

in stead of reassigning

edit: angular extend works on objects, not arrays, so replaced it with a bit of JS.

Upvotes: 0

Related Questions