简体   繁体   English

在我的情况下如何传递变量?

[英]How to pass variables in my case?

I am trying to decide the best practice for my controller. 我正在尝试确定控制器的最佳实践。

I have multiple controller and I need to pass data around. 我有多个控制器,我需要传递数据。

first controller 第一控制人

app.controller('testCtr', ['$scope','$rootScope', function($scope, $rootScope) {
    $scope.testdata = true;
}])

second controller 第二控制器

app.controller('secondCtr', ['$scope','$rootScope', function($scope, $rootScope) {
    var test = $rootscope.testdata;

}])

My second option is to use $parent 我的第二个选择是使用$ parent

first controller 第一控制人

app.controller('testCtr', ['$scope','$rootScope', function($scope, $rootScope) {
    $scope.testdata = true;
}])

second controller 第二控制器

app.controller('secondCtr', ['$scope','$rootScope', function($scope, $rootScope) {
    var test = $scope.$parent.$parent.testdata;
}])

I am not sure what's the best way to do this. 我不确定执行此操作的最佳方法是什么。 Anyone? 任何人? Thanks! 谢谢!

You could instead notify another controller using eventing. 您可以改为使用事件通知另一个控制器。

Example:- 例:-

app.controller('testCtr', ['$scope','$rootScope', function($scope, $rootScope) {
    $scope.testdata = true;
    $rootScope.$broadcast('DATA_UPDATED', $scope.testdata);
}]);

and receive using 并使用

app.controller('secondCtr', ['$scope','$rootScope', function($scope) {
    $scope.$on('DATA_UPDATED', function(e, data){
        testData = data;
        //do something with the data
    });
}]);

Again this completely depends upon the how the controller is arranged, and instead of passing around the data you could use a service (which are singletons) which manages the data . 同样,这完全取决于控制器的布置方式,而不是传递数据, 您可以使用管理数据的服务(单例) So one controller can just notify other controller that "I have updated now you go get it". 因此,一个控制器可以仅通知另一控制器“我已经更新了,现在您可以获取它”。 Other controller would have injected the dependency on same service which shares and manages the data, on receiving the notification of the event it can call the service and get the updated data. 其他控制器将在对共享和管理数据的同一服务的依赖项上注入依赖,一旦收到事件通知,它就可以调用该服务并获取更新的数据。 Here is another good read on various services options in angular 这是有关Angular各种服务选项的又一好读

Again use of $rootScope to $broadcast (downwards) or $emit(upwards) or just using the $scope itself to do it depends upon how the controllers are structured ($rootScope.broadcast helps resolve that, but you need to inject $rootScope which sometimes is inconvenient). 再次使用$rootScope进行$broadcast (downwards)$emit(upwards)还是仅使用$scope本身来完成此操作取决于控制器的结构($ rootScope.broadcast可帮助解决该问题,但您需要注入$ rootScope有时不方便)。 But it is always better to have same implementation everywhere without worrying about how the controllers are structured, i have a pub/sub mechanism created for that, which i generally use and you can find it here . 但是在任何地方都具有相同的实现总是更好,而不必担心控制器的结构, 我为此创建了一个pub / sub机制,我通常会使用它,您可以在这里找到它 Another thing to worry when you share data between controllers are if you are passing around objects then the reference of the object is copied on the target, so any update on the target will reflect the change at source itself, some cases it is not desirable so you could angular.copy(data) the data at the source while sending it. 在控制器之间共享数据时,还要担心的另一件事是,如果您要传递对象,那么对象的引用将被复制到目标上,因此目标上的任何更新都将反映源本身的更改,在某些情况下,这是不可取的,因此您可以在发送数据时在源处对数据进行angular.copy(data)

Also note that scopes follow prototypical inheritance pattern, ie all scopes(except isolated scope, $scope.$new(true) ) are inherited from its parent, ultimately they all are derived from the $rootScope which means a child scope can access the property set at its ancestors (via prototype chain). 还要注意,作用域遵循原型继承模式,即所有作用域(独立作用域, $scope.$new(true)除外)均从其父项继承,最终它们都从$rootScope派生, 这意味着子作用域可以访问该属性设置在其祖先(通过原型链)。

Ultimately there are numerous ways to pass data, notify, share/access data between different components. 最终,有许多方法可以在不同组件之间传递数据,通知,共享/访问数据。

The best way is to use a service or factory. 最好的方法是使用服务或工厂。 You can check this video from egghead.io Services are singleton objects, this means that they only have one instance. 您可以从egghead.io检查此视频。服务是单例对象,这意味着它们只有一个实例。 Once you make an instance of a service in a controller you can not make a new one in another, instead you will get the reference of the existing instance and so have access to the data from multiple controllers! 在控制器中创建服务实例之后,就不能再创建一个服务实例,相反,您将获得现有实例的引用,因此可以从多个控制器访问数据!

Look at the video, he can explain it better than I can ;) 观看视频,他可以比我更好地解释;)

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

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