使用服务在控制器之间共享对象

时间:2017-06-08 14:16:28

标签: javascript angularjs

我必须控制器第一个控制器是" cockpitController"另一个" idCardSupplierWarnController" 。在第一个控制器中我设置了我的对象,我检查了设置是否正常工作,我可以看到所有对象,但是当我想在另一个控制器中获取对象时,我的所有对象都为空。

PS :我检查了这个解决方案它是否适用于控制器位于导航器的同一个窗口中但在我的情况下它在新窗口中使用{ {1}}。

Le service idCardSupplierWarnService

$window.open(url)

Le controller cockpitController

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 ** idCardSupplierWarnController :**

    (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;
                    });
                }

            }]);
    })();

Result after the first set in CockpitController

2 个答案:

答案 0 :(得分:1)

"这"在您的服务功能中,指的是您服务中的各个功能,而不是服务本身。

将您的服务修改为如下所示:

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

});

答案 1 :(得分:0)

您需要将idCardSupplierWarn模块注入cockpit模块,才能访问service

angular.module("cockpit", ['mm.foundation', 'security', 'message', `isteven-multi-select`, `idCardSupplierWarn`])
相关问题