-1

I am trying to use data in a redirected page which is redirected in angular. How can I move data in this case. I tried using in rootscope but is this preferable? is ther any way where I can use the data from first page and use it in the page after redirection?

var app = angular.module('urlApp', []);
app.controller('urlCtrl', function ($scope, $log, $window) {
    $scope.ClickMeToRedirect = function () {
    $scope.myData = datafrommyDb;//This is what I am planning to use in redirected page
        var url = "http://" + $window.location.host + "/Account/Login";
        $log.log(url);
        $window.location.href = url;
    };
});
Kurkula
  • 6,386
  • 27
  • 127
  • 202

2 Answers2

1

If you are using angular-ui-router than you can follow below approach

add params attribute in .state

.state("nextPage", {
        url: '/nextpage',
        templateUrl: 'app/views/next.html',
        controller: 'NextController',
        controllerAs: 'vm',
        params: {
            alpha: null,
            beta: null
        },
        data: {
            pageTitle: 'Next Page'
        }
    });

now on current page

 var params = { alpha :'alpha', beta:  'beta'}
 $state.go('nextPage', params);

on the nextpage

 angular
     .module('app')
     .controller('NextController', function($log, $state) {
         var vm = this;

         $log.log(' $state.params', $state.params);

         if ($state.params.alpha !== null && $state.params.beta !== null) {
             vm.whatevervariable = $state.params.alpha;
             vm.anothervariable = $state.params.beta;
         }
});
xkeshav
  • 53,360
  • 44
  • 177
  • 245
1

If you want use data across different controllers, you can use angular service. Since service are singleton object, the same object is getting injected to different controllers. $rootScope is not preferred because it is common to all the controllers and we might not be using the data in all the controllers. check this links

$rootScope vs. service - Angular JS

Share data between AngularJS controllers

Community
  • 1
  • 1
Jithu Rajan
  • 452
  • 3
  • 14