0

I'm trying all the approaches passing data between controllers using service, factory or broadcast. None of them works for me. I follow the exact solution online, but still unfortunate. I placed service inside my app.js.

App.JS

myApp.service('customService', [function () {
    this.list = [];

    this.setObject =  function (o) {
        this.list.push(o);
    },
    this.getObject =  function () {
        return this.list;
    }
}]);

Controller #1

myApp.controller('Controller1', function ($scope, customService) {
    customService.setObject({..});
    $window.open("/controller2", '_blank');
}

Controller #2

myApp.controller('Controller2', function ($scope, customService) {
 console.log(customService.getObject()); // Returns []
}

Problem

It returns [] on controller 2 from controller 1, instead of object data.

Foonation
  • 215
  • 3
  • 17

3 Answers3

0

Are your controllers in the same page ?

Angular.js only works and keeps data on a single page. If your page reloads (as you seem to indicate when you say "express.js loads the next page", then it reinitialized everything.

You should either:

look at how to use Angular.js routing (http://docs.angularjs.org/tutorial/step_07) so that you stay on the same page. use something to persist your data, such as localstorage. Find out more: http://diveintohtml5.info/storage.html

ref : Using angular service to share data between controllers

Community
  • 1
  • 1
Tutupe
  • 11
  • 1
  • 3
0

You should modify your service for storing a object under some specific key, and then retrieve it later given that key. You can define these keys whatever you like. I defined them in the same service so I can reuse them through all controllers. Something like this

Service

myApp.service('customService', [function () {
    this.keys = {"foo": "foo", "bar": "bar"};
    this.list = {};

    this.setObject =  function (obj, key) {
        this.list[ley] = obj;
    },
    this.getObject =  function (key) {
        return this.list[key];
    }
}]);

Controller #1

myApp.controller('Controller1', function ($scope, customService) {
    customService.setObject({"propX": "propX"}, customService.foo);
    //$window.open("/controller2", '_blank');
    /* I encourage you to use something like ngRoute here for navigating
     * so, you should do something like $location.path('/controller2');
     */
}

Controller #2

myApp.controller('Controller2', function ($scope, customService) {
 console.log(customService.getObject(customService.foo));
}
lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
  • It doesn't work. Second controllers returns "undefined", even though I had correct `this.list[key] = obj;` – Foonation Mar 10 '17 at 15:52
  • How are you getting to `/controller2` route ? are you using _ngRoute_? I mean, how it's instantiated **Controller2**? the page should not be reloaded, neither you can open it in another tab. Please confirm this. – lealceldeiro Mar 10 '17 at 15:56
  • @Foonation, I didn't realised in **Controller1** I just copied and pasted the `$window.open("/controller2", '_blank');` part, but I encourage you to use a different approach here. See my answer edited, please – lealceldeiro Mar 10 '17 at 16:00
  • @Foonation, great! – lealceldeiro Mar 15 '17 at 12:46
0

have you used routing? if u use then your code should be work.

  • I'm using RouteProvider when instead of state. – Foonation Mar 10 '17 at 23:20
  • if you want to change page like $window.open("/controller2", '_blank'). that time your data can't pass on controller2. because that time your app is reload and services create new different instances. – Rahul Nikam Mar 12 '17 at 12:35