在angular js中使用一个控制器的变量值到另一个控制器

Using the value of variable of one controller into another controller in angular js

本文关键字:控制器 一个 变量值 另一个 js angular      更新时间:2023-09-26

我正在使用Angular js,我需要将变量值从一个控制器发送到另一个控制器,就像全局变量概念一样,但我现在无法做到这一点,所以请建议如何解决这个问题。我附上js的代码供参考。这里是我想从index.js到TeethClick.js使用的牙数

我正在尝试从index.js发送teethnumber的值

 (function () {
    appModule.controller('tenant.views.dentalchart.index', [
        '$scope', '$modal', 'abp.services.app.patients',
        function ($scope,$modal,patientsService) {
            var vm = this;
            var teethnumber = "";
 vm.openTeethClick = function (teeth) {
                teethnumber = teeth;
                console.log(teethnumber);
                var modalInstance = $modal.open({
                    templateUrl: '~/app/tenant/views/dentalchart/TeethClick.cshtml',
                    controller: 'tenant.views.dentalchart.TeethClick as vm',
                    backdrop: 'static'
                });
                modalInstance.result.then(function () {
                    getPatient();
                });
            };
 }
    ]);
})();

我想访问牙齿数到下面的控制器teethClick.js

(function () {
    appModule.controller('tenant.views.dentalchart.TeethClick', [
        '$scope', '$modalInstance', 'abp.services.app.patients',
        function ($scope, $modalInstance, patientsService) {
            var vm = this;
            //var teethnumber = $scope.teethnumber;
            vm.saving = false;
            vm.patient = {};
            var teethnumber = 1;
            vm.save = function () {
                alert(vm.patient);
                vm.saving = true;
                patientsService.addPatient(vm.patient).success(function () {
                    abp.notify.info(app.localize('SavedSuccessfully'));
                    $modalInstance.close();
                }).finally(function () {
                    vm.saving = false;
                });
            };
           vm.getDefaultSurfaceID = function (teeth) {
               var surfaceDefault = teeth;
               console.log(surfaceDefault);
               if (teethnumber != null && surfaceDefault != null)
               {
               }
            };
            vm.cancel = function () {
                $modalInstance.dismiss();
            };
        }
    ]);
})();

请建议如何解决我的这个问题

创建一个angular service来同步你想要的数据。这里有一个简单的例子。

angular.module('myApp', [])
    .controller('Ctrl1', function ($scope, App) {
        $scope.localData1 = App.data;
    })
    .controller('Ctrl2', function ($scope, App) {
        $scope.localData2 = App.data;
    })
    .factory('App', function () {
        var sharedObj = {
            data : {
                status: 'Good'
            }
        };
        return sharedObj;
    });