AngularJS 服务在控制器之间传递数据 [英] AngularJS Service Passing Data Between Controllers

查看:29
本文介绍了AngularJS 服务在控制器之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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(定义服务的地方)

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

});

推荐答案

像这样定义你的服务

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- 在 angular 中声明服务的不同方式的主要区别是什么?

这篇关于AngularJS 服务在控制器之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆