我非常烦恼,我无法存储下一个视图的用户信息。这是我的工厂:
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
}
}
我使用以下函数设置数据:
currentUser.setProfile(data);
但是当我在另一个控制器中尝试获取数据时,我得到了空白对象:
currentUser.getProfile();
这是我的路由
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"
})
请帮我解决我最早需要解决方案的问题。
答案 0 :(得分:1)
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();
}]);