在Angular JS中的控制器之间共享数据? [英] Sharing data between controllers in Angular JS?

查看:98
本文介绍了在Angular JS中的控制器之间共享数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Before this is marked as duplicate I've read quite of few similar questions, but all the answers I've found seem to use $scope, and after reading the documentation I'm not really sure I understand $scope, or why I'd use it in this situation.

我发现本教程,其中介绍了如何执行我要执行的操作。

I found this tutorial which describes how to do what I'm trying to do.

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

However, it's using an array of data. I just need one solid variable. In addition, I don't know why he's declaring an additional object to the factory service he creates; why not just use the factory as the object?

我以为可以做这样的事情,但是我不确定它是否可以工作。

I was thinking I could do something like this, but I'm not sure if it will work or not.

创建我的工厂/服务:

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

demoModule.factory("demoService", function() {
     var demoSharedVariable = null;
     return demoSharedVariable;
});

访问每个控制器中的共享变量:

Accessing the shared variable in each controller:

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

此方法会产生我所追求的共享变量吗?

Will this method produced the shared variable I'm after?

推荐答案

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

You need to inject the service in order to use it, then access the service variable.

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

如果您使用controllerAs,则很少(或不需要)注入和使用$ scope 。由于controllerAs是一个相对较新的功能,那时我们别无选择,只能使用$ scope,因此用$ scope查找示例并不奇怪。

If you are using controllerAs, you rarely (or shouldn't) need to inject and use $scope. As controllerAs is a relatively newer feature, back then we have no choice but to use $scope, so it is not strange to find example with $scope.

编辑::如果您未使用controllerAs(如本例所示),则需要$ scope才能向视图公开函数或变量。

If you are not using controllerAs (like in this example) you would need $scope to expose functions or variables to the view.

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

There are several place that are not correct I've found while fiddling with it, I'll edit the code. I don't know how to showcase the effect without using advanced concept like $watch, please provide your own fiddle if you don't understand.

Jsbin

这篇关于在Angular JS中的控制器之间共享数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆