简体   繁体   中英

Not getting stored data when navigating to next view

I am very much troubled by the thing, tat I am not able to store the user info for the next view. Here is my factory:

app.factory("currentUser", currentUser);
function currentUser() {
    var profile = {
        userId: "",
        firstName: "",
        lastName: "",
        userEmailId: "",
        userAccessToken: ""
    };

    var setProfile = function (data) {
        profile.userId = "";
        profile.firstName = data.userDetails.frstName;
        profile.lastName = data.userDetails.lastName;
        profile.userEmailId = data.Email;
        profile.userAccessToken = data.userDetails.accessCode;
    }

    var getProfile = function () {
        return profile;
    }

    return {
        setProfile: setProfile,
        getProfile: getProfile
    }
}

I set the data using the function:

currentUser.setProfile(data);

But when in another controller I am trying to get the data I am getting blank object:

currentUser.getProfile();

Here is my routing

  angular.module("angularModule", ["ngAnimate", "ui.router"])
             .config(function ($stateProvider, $urlRouterProvider, $locationProvider) {

                 // catch all route
                 // send users to the form page 
                 $locationProvider.html5Mode(true);
                 $stateProvider.state('Register', {
                       url: '/Register',
                       templateUrl: 'Templates/Registration.html',
                       controller: "Registration"
                   })

Please help me with the solution I need the solution at earliest.

var app = angular.module("MyApp", []);
app.factory("myService", function() {
  var profile = {
    userId: "",
    firstName: "",
    lastName: "",
    userEmailId: "",
    userAccessToken: ""
  };
  profile.setProfile = function(userDetails) {
    profile.userId = "";
    profile.firstName = userDetails.frstName;
    profile.lastName = userDetails.lastName;
    profile.userEmailId = userDetails.userEmailId;
    profile.userAccessToken = userDetails.accessCode;
  }
  profile.getProfile = function() {
    return profile;
  }
  return profile;
});
app.controller("ctrl1", ["$scope", "myService", function($scope, myService) {
  $scope.profile = {
    userId: "1233",
    firstName: "JOHN",
    lastName: "DOE",
    userEmailId: "",
    userAccessToken: "314wdw"
  };
  myService.setProfile($scope.profile);
  $scope.valueA = myService.getProfile();
}]);
app.controller("ctrl2", ["$scope", "myService", function($scope, myService) {
  $scope.valueB = myService.getProfile();
}]);

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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