简体   繁体   English

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

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

I'm newbie to AJs, and I'm trying to share $scope.showdropdown directive with another controller so that the second controller can call this directive in its body. 我是AJ的新手,并且尝试与另一个控制器共享$ scope.showdropdown指令,以便第二个控制器可以在其主体中调用此指令。

this is what I want: 这就是我要的:

.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!!! 
   }
})

I tried all sorts of thing, factory etc. and had no luck Can someone show me how can this be done please! 我尝试了各种事情,工厂等,但没有运气。有人可以告诉我如何做到这一点!

If it's only simple data that you need to share between the controllers (ie no functionality, validation, or other model logic), you can use a value service: 如果只需要在控制器之间共享简单数据(即没有功能,验证或其他模型逻辑),则可以使用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

Demo 演示版

The concept is the same for a factory or service service type and the implementation is very similar. factoryservice服务类型的概念相同,实现也非常相似。 Go with one of these if you need to share functionality, not just data. 如果您需要共享功能,而不仅仅是数据,请选择其中之一。 Factory example: 工厂示例:

.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

If you need to access a scope outside of the parent scope (in this example) – DON'T !!! 如果您需要访问父范围之外的范围(在此示例中),请不要! Configure your code so that the scope you need is available in your current scope – or you might create a service to store values you'll need later on. 配置代码,以使所需的作用域在当前作用域中可用–或者您可以创建服务来存储以后需要的值。 For instance. 例如。 You might have a UserService. 您可能有一个UserService。 When someone logs in you save the user object to the UserService, then somewhere else when you need to get the user object you call the UserService.getCurrentUser() … don't go trying to find it on some scope outside of your current heirarchy. 当有人登录时,将用户对象保存到UserService,然后在需要获取用户对象的其他地方,调用UserService.getCurrentUser()……不要尝试在当前层次结构之外的某个范围内查找它。

If you are writing a directive you will likely create an isolate scope. 如果要编写指令,则可能会创建一个隔离范围。 So, in your directive pass in as an argument any scope values you may need and place them in the isolate scope. 因此,在您的指令中,将您可能需要的所有作用域值作为参数传递,并将它们放在独立作用域中。

It is an extremely rare case where you will need to use $parent or $rootScope and should avoid their use 99.9999% of the time. 在极少数情况下,您将需要使用$ parent或$ rootScope,并且应避免在99.9999%的时间内使用它们。

Or.. you can use $rootScope if you want some shared states/methods in your app.. 或者..如果您想在应用程序中使用某些共享状态/方法,则可以使用$ rootScope。

.controller('firstCtrl', function($rootScope){
    $rootScope.shared = 123;

})

.controller('secondCtrl', function($rootScope){
   console.log($rootScope)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM