0

I have to controllers the first controller is "cockpitController"and the other one "idCardSupplierWarnController" .In the first controller i set my objects and i checked if the set work and it work i can see all my objects but when i want to get my objects in the other controller then all my objects are null .

PS: I checked this solution it's working for the case that the controller is in the same Window of the navigator but in my case it's in new window using $window.open(url).

Le service idCardSupplierWarnService :

var app = angular.module('idCardSupplierWarn');

app.service('idCardSupplierWarnService', function () {

    this.idRefNum = "";
    this.idSupNum = "";
    this.codeSuppNum = "";

    this.setParam = function (paramSet) {

        console.log(paramSet);

        this.idRefNum = paramSet.designRefPart;
        this.idSupNum = paramSet.idSuppNumber;
        this.codeSuppNum = paramSet.codeSupp;

    };

    this.getParamSupNum = function () {

        return this.idSupNum;

    };

    this.getParamCodeSupNum = function () {

        return this.codeSuppNum;

    };

    this.getParamIdRefNum = function () {

        return this.idRefNum;

    };


});

Le controller cockpitController :

    (function () {
        angular
            .module("cockpit", ['mm.foundation', 'security', 'message', "isteven-multi-select"])
            .controller('cockpitController', ['$scope', '$translate', 'serviceCockpit','idCardSupplierWarnService', '$window', function ($scope, $translate, serviceCockpit,idCardSupplierWarnService,$window) {

                var urlSuppliersWarning = 'rest/suppliers/warnings';
                var urlSuppliersWarningByRefForDetails = 'rest/suppliers/warnings/supplier/ref/search';


                var self = this;

                serviceCockpit.loadData([urlSuppliersWarning]).then(function (results) {
                    self.suppliersWarning = results[0].data;
                });

                this.change = function () {

                    if (this.openWindow) {
                        this.openWindow = false;
                    }
                    else {
                        this.openWindow = true;
                    }

                };


                $scope.openNewWindowRef = function (url, params) {
                    console.log(params);
                    idCardSupplierWarnService.setParam(params);
                    console.log(idCardSupplierWarnService.getParams());
                    $window.open(url, '_blank', 'left=0, top=0, width=1100,height=600,scrollbars=yes, resizable=1');
                };

                $scope.openNewWindowSupp = function (url, params) {
                    idCardSupplierWarnService.setParam(params);
                    console.log(idCardSupplierWarnService);
                    $window.open(url, '_blank', 'left=0, top=0, width=1100,height=600,scrollbars=yes, resizable=1');
                };

                this.process = function (items) {

                    if (items.origin == 'reference' || items.origin == 'suppliers' || items.origin == 'supplierAccounts' || items.origin == 'supplierAddressCodes' || items.origin == 'reset') {

                        serviceCockpit.loadData([urlSuppliersWarningByRefForDetails], items).then(function (results) {
                            self.suppliersWarningDetails = results[0].data;
                        });
                    }

                    serviceCockpit.loadData([urlSuppliersWarning], items).then(function (results) {
                        self.suppliersWarning = results[0].data;
                    });
                }

            }]);
    })();

Le controller **idCardSupplierWarnController :**

 (function () {
    angular
        .module("idCardSupplierWarn", ['mm.foundation', 'security', 'message', "isteven-multi-select"])
        .controller('idCardSupplierWarnController', ['$translate', '$scope', 'serviceCockpit','idCardSupplierWarnService', function ($translate, $scope, serviceCockpit,idCardSupplierWarnService) {


            var urlSupplierWarningByRefDetail = 'rest/suppliers/warnings/supplier/details';

            var self = this;


             var params = {} ;

             params.idRefNum = idCardSupplierWarnService.getParamIdRefNum();
             params.idSupNum = idCardSupplierWarnService.getParamSupNum();
             params.codeSuppNum = idCardSupplierWarnService.getParamCodeSupNum();
                      console.log(params.codeSuppNum);


            serviceCockpit.loadData([urlSupplierWarningByRefDetail], params).then(function (results) {
                self.suppliersWarningsList = results[0].data;
            });


        }]);
})();

Result after the first set in CockpitController

YasBES
  • 2,365
  • 3
  • 21
  • 33
  • First, please keep it in english. Next, use a factory to share data between controllers. You missed the right behavior of an service. You using a service like a factory. What you looking for is a factory. – lin Jun 08 '17 at 14:17

2 Answers2

1

"This" in the functions of your service refers to the individual functions in your service, not the service itself.

Modify your service to look like this:

app.service('idCardSupplierWarnService', function () {

    var service = this
    service.idRefNum = "";
    service.idSupNum = "";
    service.codeSuppNum = "";

    service.setParam = function (paramSet) {

        console.log(paramSet);

        service.idRefNum = paramSet.designRefPart;
        service.idSupNum = paramSet.idSuppNumber;
        service.codeSuppNum = paramSet.codeSupp;

    };

    service.getParamSupNum = function () {

        return service.idSupNum;

    };

    service.getParamCodeSupNum = function () {

        return service.codeSuppNum;

    };

    service.getParamIdRefNum = function () {

        service this.idRefNum;

    };

    return service

});
Mike Feltman
  • 5,160
  • 1
  • 17
  • 38
  • same problem when i set the parameters in the cockpitController it works but when i invoke the service in idCardSupplierWarnController ,all my objects are null – YasBES Jun 08 '17 at 14:28
  • It is probably an order of execution issue. Since the URL is hard-coded and you want the data available all of the time, I would suggest retrieving it in the constructor of the service. – Mike Feltman Jun 08 '17 at 14:32
  • can you please give me some exemples – YasBES Jun 08 '17 at 14:46
0

You need to inject idCardSupplierWarn module into cockpit module, to access the service.

angular.module("cockpit", ['mm.foundation', 'security', 'message', `isteven-multi-select`, `idCardSupplierWarn`])
anoop
  • 3,812
  • 2
  • 16
  • 28
  • i changed as your solution but nothing change – YasBES Jun 08 '17 at 14:39
  • @YasBES : any error in browser console ? – anoop Jun 08 '17 at 14:39
  • No error just for the first controller CockpitController i can set and when i check the get methods it works fine but when i try to get the objects in the other controller idCardSupplierWarnController i got nothing (null objects) – YasBES Jun 08 '17 at 14:41
  • @YasBES : after adding the dependecy of `idCardSupplierWarn` module into `cockpit` module, could you confirm that `setParam()` method of service gets called before `getparam...()` methods ?? – anoop Jun 08 '17 at 14:47
  • Yes sure , i tested the getParam() in the first controller CockpitController it's work fine but when i called the same method in the other controller it didn't work – YasBES Jun 08 '17 at 14:52
  • @YasBES : I mean, could you confirm that `setparam()` is called before `getparam()` of `idCardSupplierWarnController`?? – anoop Jun 09 '17 at 08:15
  • @annop yes sure – YasBES Jun 09 '17 at 09:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146237/discussion-between-anoop-and-yasbes). – anoop Jun 09 '17 at 09:05