如何在angularjs中的两个控制器之间共享$scope属性,而不是其值

How to share $scope properties, not its value between two controllers in angularjs?

本文关键字:属性 scope 共享 之间 控制器 angularjs 两个      更新时间:2023-09-26

我是AJ的新手,我正在尝试与另一个控制器共享$scope.showdropdown指令,以便第二个控制器可以在其主体中调用该指令。

这就是我想要的:

.controller('firstCtrl', function($scope){
    $scope.showdropdown = false; // this is part of this controller 
})
.controller('secondCtrl', function($scope){
   if(username and pass correct){
      $scope.showdropdown = true; // I want to call this here, but I can't do it bcoz its not part of the same controller!!! 
   }
})

我尝试了各种各样的东西,工厂等等,但都没有成功有人能告诉我怎么做吗!

如果只是需要在控制器之间共享的简单数据(即没有功能、验证或其他模型逻辑(,则可以使用value服务:

.controller('firstCtrl', function($scope, myVal){
    $scope.shared = myVal; // 'shared' on scope now references value service
    $scope.shared.showdropdown = false;
})
.controller('secondCtrl', function($scope, myVal){
   $scope.shared = myVal;
   if(username and pass correct){
      $scope.shared.showdropdown = true; // updates myVal value
   }
})
.value('myVal', {}) // register service, initializing with an object object

演示

factoryservice服务类型的概念是相同的,并且实现非常相似。如果您需要共享功能,而不仅仅是数据,请使用其中一个。工厂示例:

.controller('firstCtrl', function($scope, MyFactory){
    $scope.shared = MyFactory;
    ...
})
.controller('secondCtrl', function($scope, MyFactory){
    $scope.shared = MyFactory;
    ...
})
.factory('MyFactory', function(){
    var obj = {
        //showdropdown: false
    };
    obj.someFunc = function(){};
    return obj;
})
<!-- Nested Scopes -->
<div ng-controller="ParentCtrl">
<p>Parent Scope</p>
<ul>
    <li> {{ford}}</li>
    <li> {{chevy}}</li>
    <li>{{dodge}}</li>
</ul>
<div ng-controller="ChildCtrl">
    <p>Child Scope</p>
    <ul>
        <li> {{apple}}</li>
        <li> {{samsung}}</li>
        <li>{{motorola}}</li>
    </ul>
    <div ng-controller="AnotherChildCtrl">
        <p>2nd Child Scope</p>
        <ul>
            <li> {{boeing}}</li>
            <li> {{grumman}}</li>
            <li>{{lockheed}}</li>
        </ul>
        <p>Combination of three scopes</p>
        <ul>
            <li> From parent - {{ ford }}</li>
            <li> From child - {{ motorola }}</li>
            <li> From anotherChild  - {{ lockheed }}</li>
        </ul>
    </div>
</div>

The result looks like this …
Parent Scope
•   Mustang
•   Corvette
•   Charger
Child Scope
•   iPhone
•   Galaxy
•   MotoX
2nd Child Scope
•   747
•   F-14
•   F-35
Combination of three scopes
•   From parent - Mustang
•   From child - MotoX
•   From anotherChild - F-35

如果您需要访问父作用域之外的作用域(在本例中(,请不要!!!配置您的代码,使您需要的作用域在当前作用域中可用,或者您可以创建一个服务来存储以后需要的值。例如。您可能有UserService。当有人登录时,您将用户对象保存到UserService,然后当您需要获取用户对象时,请在其他地方调用UserService.getCurrentUser((……不要试图在当前继承权之外的某个范围内找到它。

如果您正在编写指令,则可能会创建一个隔离作用域。因此,在您的指令中,将您可能需要的任何范围值作为参数传递,并将它们放置在隔离范围中。

在极少数情况下,您需要使用$parent或$rootScope,并且应在99.9999%的时间内避免使用它们。

或者。。如果你想在你的应用程序中共享一些状态/方法,你可以使用$rootScope。。

.controller('firstCtrl', function($rootScope){
    $rootScope.shared = 123;
})
.controller('secondCtrl', function($rootScope){
   console.log($rootScope)
})