0

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

code Page1.html

var app = angular.module("app", ["xeditable", "angularUtils.directives.dirPagination", "ngNotify", "ngCookies","ngRoute"]);
app.controller('Controller1', ['$scope', '$http', '$window', '$filter','$notify','$cookieStore',
 function ($scope, $http, $window, $filter, $notify, $cookieStore)
 {
   $scope.Message="Hi welcome"

 }]);

now i want to show scope message into page2 controller

 var app = angular.module("app", ["xeditable", "angularUtils.directives.dirPagination", "ngNotify", "ngCookies","ngRoute"]);
    app.controller('Controller2', ['$scope', '$http', '$window', '$filter','$notify','$cookieStore',
     function ($scope, $http, $window, $filter, $notify, $cookieStore)
     {
       ///here i want get that scope value

     }]);
3

4 Answers 4

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

yes i need service give sample @vahid najafi or please elabrate the root scope
About $rootScope, when you created it as mentioned in the answer, you can access it like $scope. And for service take a look at here airpair.com/javascript/posts/…
0

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());
 }]);

3 Comments

i get an a error look like this Error: [$injector:unpr]
i get undefined yr
you probably forgot to add msgService in the controllers dependancies list. see at the end of the above solution.You also need to add the service file in your index.html.
0

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()

14 Comments

cookie will not reflect imediately once page refresh then only it's work
i have one dropdown in page 1 when i select the value mens reflects the page2 controller
You should use $watch for it . But best practice is use services or factory
i have confused give some fiddle or sample code yar
I dont have a dropdown or routing now . Can you create plnkr or fiddle ?
|
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.