【问题标题】:AngularJS Service Passing Data Between ControllersAngularJS 服务在控制器之间传递数据
【发布时间】:2013-07-30 22:19:30
【问题描述】:

当使用 AngularJS 服务尝试在两个控制器之间传递数据时,我的第二个控制器在尝试从服务访问数据时总是收到 undefined。我猜这是因为第一个服务执行 $window.location.href 并且我认为这是清除服务中的数据?有没有办法让我将 URL 更改为新位置并将数据保留在第二个控制器的服务中?当我在第二个控制器中运行警报下方的代码时,总是未定义。

app.js(定义服务的地方)

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:(在服务中设置价值的控制器 1)

    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(第二个控制器尝试从服务中读取值)

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

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

});

【问题讨论】:

    标签: angularjs angularjs-service


    【解决方案1】:

    这样定义你的服务

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

    在这里查看邓肯的回答:

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

    【讨论】:

    • 感谢@Josh-Petitt。我仍然不确定,但我现在认为这是因为在第二页上我转换了一个完整的 HTML 文档,我在 html 元素中有一个 ng-app 指令。所以我猜这就是清除服务数据的原因?听起来对吗?
    • 也许,如果不看整个应用程序就很难说。 FWIW,这是我一直在学习的 AngularJS 项目。你可能会在 app/user 目录下浏览一下,看看我是如何实现它的。我可以在不同的页面上使用 userService。 github.com/jpmec/done
    • @user1093077 您好,您的解决方案是否得到解决。我有同样的问题:stackoverflow.com/questions/20118335/…
    猜你喜欢
    • 2014-01-30
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多