Javascript 声明工厂中的问题:AngularJs
考虑下面的代码Javascript 声明工厂中的问题:AngularJs,javascript,angularjs,Javascript,Angularjs,考虑下面的代码 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'); $sc
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);
}
);
}
});
您正在用回调中的reply设置x。这就是问题所在。您正在用reply对象替换整个对象。以及指向旧x的fact.getData方法,即{}
要解决在事实对象中创建x的问题,也可以使用promise。您的工厂代码只能获取一次数据,并且无法响应控制器。我想您可能正在寻找类似这样的东西,每次调用
send()
,控制器提供回调
eb.send( "com.find.web.ed",{"Em":'user@find.com',"Pw":'123'},
function(reply){
x = reply;
});
您也可以通过$q
承诺来实现这一点
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
});
});
需要更多关于什么不起作用的信息。。。您能否确认您能够执行
eb.send()
方法并在不使用Angular的情况下填充x
?假设事件总线代码有效,您是否希望每次调用serverData.getData()
时都调用eb.send()
?@AnthonyChu,我已经更新了问题,请参见上文。
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
});
});
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;
});
});