Javascript 在Angular JS中的控制器之间共享数据?

Javascript 在Angular JS中的控制器之间共享数据?,javascript,angularjs,variables,service,controller,Javascript,Angularjs,Variables,Service,Controller,在这被标记为重复之前,我已经阅读了很多类似的问题,但是我发现的所有答案似乎都使用$scope,在阅读文档之后,我不确定我是否理解$scope,或者为什么我会在这种情况下使用它 我找到了描述如何做我想做的事情的方法 但是,它使用的是一个数据数组。我只需要一个实数变量。此外,我不知道他为什么要向他创建的工厂服务声明一个附加对象;为什么不把工厂作为对象呢 我在想我可以做这样的事情,但我不确定它是否有效 创建我的工厂/服务: var demoModule = angular.module("demoMo

在这被标记为重复之前,我已经阅读了很多类似的问题,但是我发现的所有答案似乎都使用$scope,在阅读文档之后,我不确定我是否理解$scope,或者为什么我会在这种情况下使用它

我找到了描述如何做我想做的事情的方法

但是,它使用的是一个数据数组。我只需要一个实数变量。此外,我不知道他为什么要向他创建的工厂服务声明一个附加对象;为什么不把工厂作为对象呢

我在想我可以做这样的事情,但我不确定它是否有效

创建我的工厂/服务:

var demoModule = angular.module("demoModule", []);

demoModule.factory("demoService", function() {
     var demoSharedVariable = null;
     return demoSharedVariable;
});
访问每个控制器中的共享变量:

var demoControllerOne = demoModule.controller("demoContollerOne", function(demoSharedVariable) {
     this.oneFunction = function(oneInput){
          demoSharedVariable = oneInput;
     };
});

var demoControllerTwo = demoModule.controller("demoContollerTwo", function(demoSharedVariable) {
     this.twoFunction = function(twoInput){
          demoSharedVariable = twoInput;
     };
});
myApp.controller('Ctrl2', ['$scope', 'MyService', '$window', function($scope, MyService, $window) {//inject MyService into the controller
    $scope.setShared = function(val) {
      MyService.setSharedVariable(val);
    };

    $scope.getShared = function() {
      return MyService.getSharedVariable();
    };

    $scope.alertSharedVariable = function () {
      $window.alert(MyService.getSharedVariable());
    };

}]);

此方法是否会生成我要查找的共享变量?

您需要注入服务才能使用它,然后访问服务变量

demoModule.controller("demoContollerOne", function($scope, demoService) {
  $scope.oneFunction = function(){
    demoService.demoSharedVariable = $scope.oneInput;
  };
});

demoModule.controller("demoContollerTwo", function($scope, demoService) {
  $scope.twoFunction = function(){
    demoService.demoSharedVariable = $scope.twoInput;
  };
});
如果您使用的是Controllera,则很少(或不应该)需要注入并使用$scope。由于controllerAs是一个相对较新的特性,当时我们别无选择,只能使用$scope,所以找到使用$scope的示例并不奇怪


编辑:如果不使用Controllera(如本例中所示),则需要$scope向视图公开函数或变量

有几个地方是不正确的,我发现,而摆弄它,我会编辑代码。我不知道如何在不使用$watch等高级概念的情况下展示效果,如果您不理解,请提供您自己的小提琴


重要的一点是,如果你想使用angular,你必须了解范围的知识

由于您的工厂或控制器都不正确,因此我为您编写了一个简单的示例,以帮助您了解服务:

:

服务:

angular.module('myApp').service('MyService', [function() {

      var yourSharedVariable; // Your shared variable

      //Provide the setter and getter methods
      this.setSharedVariable = function (newVal) {
        yourSharedVariable = newVal;
      };

      this.getSharedVariable = function () {
        return yourSharedVariable;
      };

    }
]);
控制器:

var demoControllerOne = demoModule.controller("demoContollerOne", function(demoSharedVariable) {
     this.oneFunction = function(oneInput){
          demoSharedVariable = oneInput;
     };
});

var demoControllerTwo = demoModule.controller("demoContollerTwo", function(demoSharedVariable) {
     this.twoFunction = function(twoInput){
          demoSharedVariable = twoInput;
     };
});
myApp.controller('Ctrl2', ['$scope', 'MyService', '$window', function($scope, MyService, $window) {//inject MyService into the controller
    $scope.setShared = function(val) {
      MyService.setSharedVariable(val);
    };

    $scope.getShared = function() {
      return MyService.getSharedVariable();
    };

    $scope.alertSharedVariable = function () {
      $window.alert(MyService.getSharedVariable());
    };

}]);

因此,为了避免遗漏任何内容,除了
demoSharedVariable
之外,我需要做的就是将
demoService
工厂添加到控制器的依赖项中?也就是说,我必须将服务添加到控制器依赖项和服务中的变量?并将服务作为数据本身的对象返回
return{demoSharedVariable:demoSharedVariable}
您失去了我。你能用一个例子更新我的代码片段吗?