AngularJs我如何使用一个控制器方法,其他控制器中的变量

问题描述:

我有两个控制器,如下图。我想用第一控制器方法/变量在其他控制器AngularJs我如何使用一个控制器方法,其他控制器中的变量

app.controller("createController", ["$scope", 
function ($scope) 
{ 
    $scope.i = 0; 
    $scope.changeEstimateStatus = function() 
    { 
    console.log('changeEstimateStatus'); 
    }; 
}]); 


app.controller("editController", ["$scope", 
function ($scope) 
{ 
    //here how can I access 'i' variable of createController 
}]); 
+0

控制器如何相关?父母/孩子 - 单独页面?提供更多的信息!\ – tymeJV 2014-10-30 13:07:05

+2

似乎是使用具有变量i的服务的地方,然后将其注入到控制器中 – user2717954 2014-10-30 13:09:32

+1

单独页面。请不要使用$ rootScope – 2014-10-30 13:09:42

使用共享服务:

app.service("mySharedService", [function() { 
    var _x = null; 

    return { 
     setX: function(val) { _x = val }, 
     getX: function() { return _x } 
    } 
}]); 

然后注入到你的控制器:

app.controller("createController", ["$scope","mySharedService", function($scope, mySharedService) { 

    $scope.i = mySharedService.getX(); //get 
    mySharedService.setX(3); //set 
}]); 
+0

Thnx非常适合您的宝贵信息。如果我有更多的方法和变量,请建议任何其他解决方案... – 2014-10-30 13:27:02

使用这一个:

1.you可以使用serveice并提出共同作用于服务和访问,其功能是所有controlller。

app.service('MyService', function() { 
    this.changeEstimateStatus = function() 
     { 
     console.log('changeEstimateStatus'); 
     }; 
}); 

    app.controller("createController", ["$scope",MyService, 
    function ($scope,MyService) 
    { 
     $scope.i = 0; 
    MyService.changeEstimateStatus(); 
    }]); 


    app.controller("editController", ["$scope", app.controller("createController", ["$scope",$rootScope, 
    function ($scope,$rootScope) 
    { 
     $scope.i = 0; 
     MyService.changeEstimateStatus(); 

    }]); 

2.您可以将该函数存储在$ rootscope object中,然后将该函数访问到所有控制器。

喜欢:

app.controller("createController", ["$scope",$rootScope, 
function ($scope,$rootScope) 
{ 
    $scope.i = 0; 

}]); 


app.controller("editController", ["$scope",$rootScope, 
function ($scope,$rootScope) 
{ 
    $rootScope.changeEstimateStatus(); 
}]); 

,但第二个选项是不是一个很好的形式给出。

+0

我同意使用服务。这很高兴知道为什么。在我看来,最好使用一个服务,因为那样你就必须在需要它的地方明确地导入这个依赖关系。如果您使用根范围,则该方法将在任何地方都可用。 – BenCr 2014-10-30 13:16:15

+1

@BenCr服务始终是放置应该在多个控制器之间共享的代码的正确位置,这正是OP所需的。应尽可能避免$ rootScope,因为它与应用程序的每一片共享,而服务只能在需要时注入。 – Blazemonger 2014-10-30 13:17:14

+0

这不完全是我说的吗?我所做的一点是,如果不告诉某人为什么不提供高质量的答案,只是说“这不是一个好方法”。 – BenCr 2014-10-30 13:26:42

你应该将通用的功能(changeEstimateStatus)到服务。两个控制器取决于服务,而不是一个控制器。

app.service('estimateService', function() { 

    this.changeEstimateStatus = function() { 
    console.log('changeEstimateStatus'); 
    }; 

}); 
    app.controller("createController", ["$scope","estimateService", 
function ($scope, estimateService) 
{ 
    $scope.i = 0; 
    $scope.changeEstimateStatus = function() 
    { 
    estimateService.changeEstimateStatus(); //delegate to the service 
    }; 
}]); 


app.controller("editController", ["$scope", "estimateService" 
function ($scope, estimateService) 
{ 
    $scope.changeEstimateStatus = function() 
    { 
    estimateService.changeEstimateStatus(); //delegate to the service 
    }; 
}]); 

另一种选择是使用$ rootScope,但使用服务并没有变得脆弱。