声明工厂中的问题:AngularJs

时间:2014-02-17 06:33:52

标签: javascript angularjs

var app = angular.module('app',[]);

//in the html, the following 2 files are attached.
//          <script src="lib/sockjs-0.3.4.min.js"></script>
//          <script src="lib/vertxbus.min.js"></script>

app.factory('serverData',function(){
    var eb = new vertx.EventBus('http://xxx.xxx.xxx.xxx');
    var x = {};
    eb.send( "com.find.web.ed",{"Em":'user@find.com',"Pw":'123'}, 
        function(reply){
            x = reply;
        });
    var fact = {};
    fact.getData = function(){
        return x;
    };

    return fact;
});

app.controller('mainController',function($scope,serverData){

});

在上面的代码中,我试图声明一个工厂从顶点服务器获取数据。它不起作用,可以帮助一些人吗?

在控制器中使用它时效果很好。见代码。

var app = angular.module('app',[]);

app.controller('mainController',function($scope,$log){
$rootScope.user = {user :'user@find.com',password :'123'};
$rootScope.reply = {};

$scope.eb = new vertx.EventBus('http://100.100.100.100:8000');

$scope.loginFunction = function(){
            $scope.eb.send( "com.find.web.ed", 
                {"Em":$scope.user.user,"Pw":$scope.user.password},
                function(reply){
                 $rootScope.reply = reply;
                 $log.warn($rootScope.reply);
} 
);
}
});

2 个答案:

答案 0 :(得分:1)

考虑以下代码

eb.send( "com.find.web.ed",{"Em":'user@find.com',"Pw":'123'}, 
        function(reply){
            x = reply;
        });

你在回调中设置了回复的x。这就是问题。您正在使用回复对象替换整个对象。而你的fact.getData方法指向旧的x,即{}。

要在事实对象中解决create x,或者也可以使用promise。

答案 1 :(得分:1)

您的工厂代码只会获取一次数据,并且无法响应控制器。我想你可能正在寻找这样的东西而不是每次调用send()并且控制器提供回调......

var app = angular.module('app',[]);

app.factory('serverData', function(){
    var eb = new vertx.EventBus('http://xxx.xxx.xxx.xxx');
    var fact = {};

    fact.getData = function(user, password, callback){
        // call send and pass it the callback function
        eb.send( "com.find.web.ed",
                 {"Em": user,"Pw": password}, 
                 callback
        );
    };

    return fact;
});

app.controller('mainController', function($scope, serverData){
    $scope.user = {user :'user@find.com',password :'123'};
    $scope.reply = {};

    serverData($scope.user.user, $scope.user.password, function (reply) {
        $scope.reply = reply;
        // might need $scope.$apply() here
    });
});

您也可以通过$q承诺...

执行此操作
var app = angular.module('app',[]);

app.factory('serverData', function($q){
    var eb = new vertx.EventBus('http://xxx.xxx.xxx.xxx');
    var fact = {};

    fact.getData = function(user, password){
        var deferred = $q.defer();
        // call send and pass it the callback function
        eb.send( "com.find.web.ed",
                 {"Em": user,"Pw": password}, 
                 function (reply) {
                     deferred.resolve(reply);
                 }
                 // also should reject on error
        );
        return deferred.promise;
    };

    return fact;
});

app.controller('mainController', function($scope, serverData){
    $scope.user = {user :'user@find.com',password :'123'};
    $scope.reply = {};

    serverData($scope.user.user, $scope.user.password)
        .then(function (reply) {
            $scope.reply = reply;
        });
});