AngularJS Service Passing Data Between Controllers

…衆ロ難τιáo~ 提交于 2019-12-17 07:12:50

问题


When using an AngularJS service to try and pass data between two controllers, my second controller always receives undefined when trying to access data from the service. I am guessing this is because the first service does a $window.location.href and I'm thinking this is clearing out the data in the service? Is there a way for me to change the URL to a new location and keep the data persisted in the service for the second controller? When I run the code below the alert in the second controller is always undefined.

app.js (Where Service is Defined)

var app = angular.module('SetTrackerApp', ['$strap.directives', 'ngCookies']);

app.config(function ($routeProvider) 
{
$routeProvider
  .when('/app', {templateUrl: 'partials/addset.html', controller:'SetController'})
  .when('/profile', {templateUrl: 'partials/profile.html', controller:'ProfileController'})
  .otherwise({templateUrl: '/partials/addset.html', controller:'SetController'});
});

app.factory('userService', function() {
var userData = [
    {yearSetCount: 0}
];

return {
    user:function() {
        return userData;
    },
    setEmail: function(email) {
        userData.email = email;
    },
    getEmail: function() {
        return userData.email;
    },
    setSetCount: function(setCount) {
        userData.yearSetCount = setCount;
    },
    getSetCount: function() {
        return userData.yearSetCount;
    }
};
});

logincontroller.js: (Controller 1 which sets value in service)

    app.controller('LoginController', function ($scope, $http, $window, userService) {

$scope.login = function() {
    $http({
        method : 'POST',
        url : '/login',
        data : $scope.user
    }).success(function (data) {
        userService.setEmail("foobar");
        $window.location.href = '/app'
    }).error(function(data) {
        $scope.login.error = true;
        $scope.error = data;
    });
}
});

appcontroller.js (Second controller trying to read value from service)

app.controller('AppController', function($scope, $http, userService) {

$scope.init = function() {      
    alert("In init userId: " userService.getEmail());
}

});

回答1:


Define your service like this

app.service('userService', function() {
  this.userData = {yearSetCount: 0};

  this.user = function() {
        return this.userData;
  };

  this.setEmail = function(email) {
        this.userData.email = email;
  };

  this.getEmail = function() {
        return this.userData.email;
  };

  this.setSetCount = function(setCount) {
        this.userData.yearSetCount = setCount;
  };

  this.getSetCount = function() {
        return this.userData.yearSetCount;
  };
});

Check out Duncan's answer here:

AngularJS - what are the major differences in the different ways to declare a service in angular?



来源:https://stackoverflow.com/questions/17952620/angularjs-service-passing-data-between-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!