使用rootScope在控制器之间共享日期

时间:2017-07-25 05:34:57

标签: controller rootscope

我有两个控制器,我想分享他们的数据。 第一个控制器是' MyController',它在图表上显示数据。 第二个控制器称为服务,并带来了一系列公司。 我想用rootScope分享数组的大小并将其显示在图表上。 非常感谢帮助

(function() {

    var module = angular.module("couponSystem");
    /**
     * MyController, Which is responsible for drawing the chart inside the admin page
     */
    module.controller('MyController', function($scope,$rootScope) {
        $rootScope.companies = [];
        var self = this;

        // chart data source
        $scope.dataSource = {
                "chart": {
                    "caption": "USER CHART",
                    "subCaption": "properties",
                    "dataFormat" :"json",
                    "xAxisName": "USERS",
                    "yAxisName": "NUMBER",
                    "paletteColors": "#0075c2",
                    "bgColor": "#ffffff",
                    "borderAlpha": "20",
                    "plotBorderAlpha": "10",
                    "placevaluesInside": "1",
                    "rotatevalues": "1",
                    "valueFontColor": "#ffffff",
                    "showXAxisLine": "1",
                    "xAxisLineColor": "#999999",
                    "divlineColor": "#999999",
                    "divLineDashed": "1",
                    "showAlternateHGridColor": "0",
                    "subcaptionFontBold": "0",
                    "subcaptionFontSize": "14"
                },
            "data" : [ {
                "label" : "No' of companies",
                "value" : $rootScope.companies.length
            },
            {
                "label" : "No' of customers",
                "value" : "11"
            },
            {
                "label" : "No' of coupons",
                "value" : "18"
            },
            // more chart data
            ]
        };
    });
})();
 second controller :




(function() {

    var module = angular.module("couponSystem");

    module.controller("GetAllCompaniesCtrl", GetAllCompaniesCtrlCtor);
    /**
     * get all Companies controller CTRL with ngConfirm - A controller that
     * allows the get all of Companies from the database, under administrator
     * permissions
     */
    function GetAllCompaniesCtrlCtor(mainAdminServiceHTTP, $ngConfirm, $state,
            $scope,$rootScope) {
        this.companies = [];
        $rootScope.companies = [];
        var self = this;
        /**
         * Get all companies function. The method receives the answer from the
         * service into the Promise, using the mainAdminService. If the
         * answer from Servis succeeded, the first function(resp) will run
         * and return the answer to the client. If the answer from the
         * service was unsuccessful, the error function will run.<br>
         * for dialog with the client i use ngConfirm
         */
        var promise = mainAdminServiceHTTP.getAllCompanies();
        promise.then(function(resp) {

            self.companies = resp.data;
            $rootScope.companies = resp.date;

        }, function(err) {
            self.error = (err.data);
            //check if the client is loged to perform actions
            if((err.status)==401){
                $ngConfirm({
                    theme : 'light',
                    animation : 'rotateYR',
                    closeAnimation : 'scale',
                    animationSpeed : 500,
                    boxWidth : '30%',
                    useBootstrap : false,
                    title : 'Login failed!',
                    content : 'You must perform a login to perform actions',
                    scope : $scope,
                    buttons : {
                        Yes : {
                            text : 'Take me to the login form',
                            btnClass : 'btn-purple',
                            /**
                             * The client will be transferred to Logain
                             * */ 
                            action : function(button) {
                            window.location.href="http://localhost:8080/CouponWebService/Login/Login.html";
                            }
                        },
                        /**
                         * The client will remain a logout and will not be able to perform operations
                         * */
                        No : function(button) {
                        },
                    }
                });

             }else{
                 self.error=err.data;
             }
        });
        /**
         * setOrder function Allows us to change the table order from large to
         * small according to the parameters given in the class html
         */
        this.orderB = "";
        this.goUp = false;

        this.setOrder = function(field) {
            this.goUp = (this.orderB != field) ? false : !this.goUp;
            this.orderB = field;
        }
    }

})();

0 个答案:

没有答案