Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在控制器之间共享数据[阵列]_Angularjs - Fatal编程技术网

Angularjs 在控制器之间共享数据[阵列]

Angularjs 在控制器之间共享数据[阵列],angularjs,Angularjs,我需要在控制器之间共享数据。我的数据实际上是一个数组。我能够成功地共享数据,但我还有一个要求。我需要从一个控制器中清除此阵列。我在sharedService中编写了一个函数sharedService.clear()。但这不起作用。我做错了什么?谁能帮我一下吗 services.service('sharedProperties', function () { var sharedService = {}; sharedService.personArray = [];

我需要在控制器之间共享数据。我的数据实际上是一个数组。我能够成功地共享数据,但我还有一个要求。我需要从一个控制器中清除此阵列。我在sharedService中编写了一个函数sharedService.clear()。但这不起作用。我做错了什么?谁能帮我一下吗

services.service('sharedProperties', function () {
   var sharedService =  {};

    sharedService.personArray = [];

    sharedService.setPersonArray = function(newObj) {
       this.personArray.push(newObj);
    };
    sharedService.getPersonArray = function(){
        return this.personArray;
    };

    sharedService.clear = function(){
        this.personArray = [];
    };
    return sharedService;

});

在定义工厂和服务的方式上,您似乎混淆了工厂和服务。请尝试以下代码:

services.service('shareProperties', function() {
 this.personArray = [];

 this.clear = function() {
  this.personArray = [];
  }
});

另外,请参阅,以了解更多详细信息,如您所解释的,您需要一个跨控制器共享人员的静态服务

services.service('sharedProperties', [function () {

    this.personArray = [];

    this.setPersonArray = function(newObj) {
       this.personArray.push(newObj);
    };
    this.getPersonArray = function(){
        return this.personArray;
    };

    this.clear = function(){
        this.personArray = [];
    };

}]);
当引用
sharedProperties
服务时,
对象上声明的任何内容都将可用。使用
var
声明某个内容将使其成为
sharedProperties
范围的私有内容,并且只能从服务中使用

在第一个示例中,
getPersonArray
将返回对
personArray
的引用,我可以更改或编辑
sharedProperties
的值,通过引用
personArray
我希望使访问方法变得毫无意义

因此,您可以这样做来保护您的
personArray

services.service('sharedProperties', [function () {

    // private
    var personArray = [];

    this.setPersonArray = function(newObj) {
       personArray.push(newObj);
       return [].concat(personArray);
    };
    this.getPersonArray = function(){
        // will return a copy of the personArray at the time of request
        return [].concat(personArray);
    };    
    this.clear = function(){
        personArray = [];
        return [].concat(personArray);
    };

}]);
这样,您只能使用您的方法编辑私有
personArray
。但它确实需要调用getPersonArray()来同步控制器之间的任何更改


我倾向于使用
工厂
作为实例对象或构造函数,而不是静态对象。

this.personArray1.push(newObj)输入错误?应该是
this.personArray.push(newObj)是输入错误。对不起,我已经更新了。
服务
是围绕
这个
对象构建的,
工厂
是围绕返回的。我相信你把这些弄混了。谢谢@Matthew.Lothian。你能告诉我做这件事的正确方法吗?@user911好的,我给了它一点解释如下:你的意思是我不需要声明var sharedService={};如果我在访问sharedProperties.getPersonArray()这样的sharedService时删除它并尝试您的代码;我得到错误“未解析的方法或函数getPersonArray()”。否,并且您也不需要返回对象。阅读我发布的链接,更深入地解释工厂、服务和供应商之间的区别。我没有重新实现您的所有功能。如果您想访问personArray,只需使用:shareProperties.PersonarrayTanks!!我是新来的,服务和工厂都搞砸了。非常感谢您澄清这个概念。为什么setPersonArray和clear中有返回语句?我认为它只适用于getPersonArray。因为我们正在更新内部
personArray
,然后返回它的副本。如果从外部更改了
sharedProperties
的值,则不会影响私有
personArray
。因此,当您设置或清除
sharedProperties
时,我们将返回更新后的
sharedProperties
谢谢!!你的回答非常详细,在澄清我的概念方面帮了我很大的忙。我想接受,但我还有最后一个问题。在控制器中访问此共享服务时,出现错误“未解析的方法或函数getPersonArray()”。如果我将其更改回旧代码,则可以访问getPersonArray()。怎么了<代码>myApp.controller('SubmitController',['$scope','sharedProperties',function($scope,sharedProperties){$scope.submitGridData=function(){alert(sharedProperties.getPersonArray();};};}])谢谢!!我接受了你的答案,因为这一条给了像我这样的新手更多的信息。