简体   繁体   English

AngularJS 工厂变量变得未定义

[英]AngularJS Factory Variable Becomes Undefined

I have a user variable within a factory which keeps becoming undefined after switching controllers.我在工厂中有一个用户变量,它在切换控制器后一直变得未定义。 I'm setting the variable in one controller.我在一个控制器中设置变量。 When I log user it appears with the data I'm expecting, but when I switch controllers and retrieve the variable it's undefined once again.当我登录用户时,它会显示我期望的数据,但是当我切换控制器并检索变量时,它再次未定义。 Here is the code:这是代码:

discussionBoard.factory('userFactory', function($http){

    console.log("I'm loading the userFactory");

    var user = {};

    var factory = {};

    factory.createUser = function(user, callback){
        $http.post('/users/new', {user: user})
            .success(function(response){
                user = response[0];
                callback(user);
            })
    }

    factory.retrieveUser = function(callback){
        callback(user);
    }

    console.log(user);

    return factory;

});

discussionBoard.controller('dashCtrl', function($scope, topicFactory, userFactory){

    $scope.createTopic = function(){
        topicFactory.createTopic(function(topic){
            $scope.topic = topic;
        })
    }

    userFactory.retrieveUser(function(user){
        $scope.user = user;
    });

});

EDIT: After a bit more experimenting I was able to get this to work by adding user as an object within the factory object.编辑:经过更多实验后,我能够通过将用户添加为工厂对象中的对象来使其工作。 This actually raises another question, however, about how the factory is interacting with AJAX here, as it seems .success() doesn't have access to the factory's scope.然而,这实际上提出了另一个问题,关于工厂如何与 AJAX 交互,因为 .success() 似乎无法访问工厂的范围。 Here's the code that worked, but I'd be curious if anyone could say why this is arranged in this way:这是有效的代码,但我很好奇是否有人能说出为什么这样安排:

discussionBoard.factory('userFactory', function($http){

    var factory = {};

    factory.createUser = function(user, callback){
        $http.post('/users/new', {user: user})
            .success(function(response){
                factory.user = response[0];
                callback(factory.user);
            })
    }

    factory.user = {};

    factory.retrieveUser = function(callback){
        callback(factory.user);
    }

    return factory;

});

discussionBoard.controller('dashCtrl', function($scope, topicFactory, userFactory){

    $scope.createTopic = function(){
        topicFactory.createTopic(function(topic){
            $scope.topic = topic;
        })
    }

    userFactory.retrieveUser(function(user){
        $scope.user = user;
    });

});

You've shadowed your factory's user variable by giving the createUser function a parameter by the same name.通过为 createUser 函数提供一个同名参数,您已经隐藏了工厂的user变量。

var user = {}; // <- this is going to be shadowed!

var factory = {};

factory.createUser = function(user, callback){ //<- note the user argument!
    $http.post('/users/new', {user: user})
        .success(function(response){
            user = response[0];
            callback(user);
        })
}

So when the success callback sets user , it finds the closest variable by that name, which is the param of the createUser function.因此,当成功回调设置user ,它会通过该名称找到最接近的变量,即 createUser 函数的参数。 Otherwise both approaches are perfectly fine - in fact the first one is better, if you want to make the user field only accessible through the getter (in the second case any controller could get a direct reference to user if the factory is injected).否则这两种方法都很好 - 实际上第一种更好,如果您想让用户字段只能通过 getter 访问(在第二种情况下,如果注入工厂,任何控制器都可以获得对用户的直接引用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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