简体   繁体   English

在控制器之间传递变量的最佳方法

[英]Best way to pass variables between controllers

I am using three Angular controllers: 我正在使用三个Angular控制器:

**Controller1**

var fetchStudentDetails = function(){
    var sDetails = myService.getList(//url-1 here);
    sDetails.then(function (data) {
            $scope.studentData = data.list;
            var studentId = $scope.studentData[0].id;
    });
}
fetchStudentDetails();
$scope.loadSecondLevel = function(){
                $state.go('secondLevel');               
              }


**Controller2**

    var fetchClassDetails = function(){
    var sDetails = myService.getList(//url-2 here);
    sDetails.then(function (data) {
            $scope.classData = data.list;
            var className = $scope.classData[0].name;
    });
    }
    fetchClassDetails();
    $scope.loadThirdLevel = function(){
        $state.go('thirdLevel');                
    }

**Controller3**

   $scope.putStudentDetails = function(){
   // Here I need studentId,className for updateResource
        var sDetails = myService.updateResource(//url-3 here);
        sDetails.then(function (data) {

        });     
    }

Where I have to pass studentId (in Controller1), className (in Controller2) into a function which in Controller3. 我必须将studentId(在Controller1中),className(在Controller2中)传递到一个在Controller3中的函数中。 I tried with $rootScope, it is working but when refresh the page $rootScope values become empty. 我尝试使用$ rootScope,它可以正常工作,但是刷新页面时,$ rootScope值变为空。 Does anyone know how to do this? 有谁知道如何做到这一点?

Your question could be split into two aspects: 您的问题可以分为两个方面:

1. How to share data between controllers 1.如何在控制器之间共享数据
The best practice to share data in Angular 1.x is using factory , store the shared data in a factory service, and expose access methods to controllers: 在Angular 1.x中共享数据的最佳实践是使用factory ,将共享数据存储在factory服务中,并向控制器公开访问方法:

factory('DetailData', function(myService, $q){

     var _details;

     function __getDetailData(){
           return details
     }

     function __setDetailData(){
          return myService.getList().then(function(data){
              _details = data;
          })
     }

     return {
         getDetailData: __getDetailData,
         setDetailData: __setDetailData
     }

})


controller('myContrller', function(DetailData, $scope){
    $scope.data = DetailData.getDetailData();
})

2. How to persist data when page refreshed, 2.如何在刷新页面时保留数据,
you can use localStorage to keep data persistent during page reloading, many tools & libraries can achieve this, for example ngStorage , or you could reset the data from server every time your angular application started: 您可以在页面重新加载期间使用localStorage来保持数据的持久性,许多工具和库都可以实现这一点,例如ngStorage ,也可以在每次启动角度应用程序时从服务器重置数据:

//this would register work which would be performed 
//when app finish loading and ready to start. 
angular.module('app').run(function(DetailData){

    DetailData.setDetailData();

})

Depending on what problem you are solving. 根据您要解决的问题。 There are three options: 共有三个选项:

  1. Is to save data to $rootScope 是将数据保存到$rootScope
  2. Is to use $scope.$emit & $scope.$on functions. 是使用$scope.$emit$scope.$on函数。
  3. Use a custom Service to store the data 使用自定义服务存储数据

And if you need to save data, so it was available after full page reload - localStorage . 而且,如果您需要保存数据,那么在整页重新加载后可以使用它-localStorage

Hey this question are responded in Passing data between controllers in Angular JS? 嘿,这个问题在Angular JS的控制器之间传递数据时得到了回答吗?

But the simple response is in the services. 但是,简单的响应就在于服务。

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

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