how to passing data from one controller to another controller using angular js 1

后端 未结 4 1891
予麋鹿
予麋鹿 2020-12-22 05:14

hi all i using angular js i need to transfer the value from one page controller to another page controller and get that value into an a scope anybody help how to do this

相关标签:
4条回答
  • 2020-12-22 05:41

    You should define a service and write getter/setter functions on this.

    angular.module('app').service('msgService', function () {
      var message;
      this.setMsg = function (msg) {
         message = msg;
      };
    
      this.getMsg = function () {
         return message;
      };
    });
    

    Now you should use the setMeg function in Controller1 and getMsg function in Controller2 after injecting the dependency like this.

    app.controller('Controller1', ['$scope', '$http', '$window', '$filter','$notify','$cookieStore', 'msgService',
       function ($scope, $http, $window, $filter, $notify, $cookieStore, msgService)
       {
         $scope.Message="Hi welcome"
         msgService.setMsg($scope.Message);
       }]);
    
    
    app.controller('Controller2', ['$scope', '$http', '$window', '$filter','$notify','$cookieStore', 'msgService',
     function ($scope, $http, $window, $filter, $notify, $cookieStore, msgService)
     {
       ///here i want get that scope value
       console.log('message from contoller 1 is : ', msgService.getMsg());
     }]);
    
    0 讨论(0)
  • 2020-12-22 05:49

    You should use services for it . Services

       app.factory('myService', function() {
     var message= [];
     return {
      set: set,
      get: get
     }
     function set(mes) {
       message.push(mes)
     }
     function get() {
      return message;
     }
    
    
    
    });
    

    And in ctrl

    ctrl1

    $scope.message1= 'Hi';
      myService.set($scope.message1);
    

    ctrl2

    var message =  myService.get()
    
    0 讨论(0)
  • 2020-12-22 05:56

    You can use $rootScope instead of $scope:

    // do not forget to inject $rootScope as dependency
    $rootScope.Message="Hi welcome";
    

    But the best practice is using a service and share data and use it in any controller you want.

    0 讨论(0)
  • 2020-12-22 06:02

    Sharing data from one controller to another using service

    We can create a service to set and get the data between the controllers and then inject that service in the controller function where we want to use it.

    Service :

    app.service('setGetData', function() {
      var data = '';
        getData: function() { return data; },
        setData: function(requestData) { data = requestData; }
    });
    

    Controllers :

    app.controller('Controller1', ['setGetData',function(setGetData) {

    // To set the data from the one controller $scope.Message="Hi welcome";
    setGetData.setData($scope.Message);

    }]);

    app.controller('Controller2', ['setGetData',function(setGetData) {
    
      // To get the data from the another controller  
      var res = setGetData.getData();
      console.log(res); // Hi welcome
    
    }]);
    

    Here, we can see that Controller1 is used for setting the data and Controller2 is used for getting the data. So, we can share the data from one controller to another controller like this.

    0 讨论(0)
提交回复
热议问题