我有一个具有多种功能的控制器。我需要这些功能来共享一些数据。例如,在下面的代码中,我的 signin 函数调用system_connect
函数,我希望将http响应数据从中返回到登录状态。
由于这两个函数都在同一个控制器中,我假设将数据放入$scope.formData
函数中的system_connect
,同样的数据可以在 signin 中访问和读取功能 - 但它不起作用。
headlessQS.controller('mainController', ['$scope', '$log', '$route', '$routeParams', '$location', '$http', '$cookieStore', function($scope, $log, $route, $routeParams, $location, $http, $cookieStore){
// signin / register function
$scope.formData = {};
$scope.signin = function(){
console.log('Signin: ');
$scope.system_connect($scope.formData);
console.log('Testing Scope.user: ');
console.log( $scope.connection_response );
$location.path('/');
}
// drupal system connect
$scope.system_connect = function(formData){
console.info('System Connect ...');
$http.post('http://mysite/angularjs-headless/api/v1/system/connect',
{
'username': formData.username,
'password': formData.password
},
{
'Content-type' : 'application/json',
'Accept' : 'application/json'
}
)
.then(
// successCallback,
function(response){
console.log('Working ...');
$log.info(response);
$scope.formData = response;
},
// errorCallback
function(response){
console.log('NOT Working ...');
$log.info(response);
$scope.formData = response;
}
);
}
}]);
如何在控制器中的两个函数之间传递数据?
感谢下面的SébastienTemprado回答,我开始看到了光明。但是,当我实现承诺时,我在尝试检查$scope.formData
的内容时遇到了问题。错误如下:
(anonymous) @ include.preload.js:628
app.js:39 Uncaught SyntaxError: Unexpected token .
由以下代码产生:
headlessQS.controller('mainController', ['$scope', '$log', '$route', '$routeParams', '$location', '$http', '$cookieStore', function($scope, $log, $route, $routeParams, $location, $http, $cookieStore){
// console.debug( $cookieStore.get('user_session') );
// re-direct to the sign-in / register form
$scope.signin = function(){
var promise = $scope.system_connect($scope.connection_response);
promise.then({
console.log("formData:"+JSON.stringify($scope.connection_response));
});
}
// drupal system connect
$scope.system_connect = function(formData){
console.info('System Connect ...');
// $http.post('/someUrl', data, config).then(successCallback, errorCallback);
var promise = $http.post('http://mysite/QuestionsGenerator/angularjs-headless/api/v1/system/connect',
{
'username': formData.username,
'password': formData.password
},
{
'Content-type' : 'application/json',
'Accept' : 'application/json'
}
)
.then(
// successCallback,
function(response){
console.log('Working ...');
$log.info(response);
$scope.connection_response = response;
},
// errorCallback
function(response){
console.log('NOT Working ...');
$log.info(response);
$scope.connection_response = response;
}
);
return promise;
}
}]);
行 39 是console.log("formData:"+JSON.stringify($scope.formData));
代码。即使取出JSON.stringify并尝试记录 $ scope.formData
答案 0 :(得分:0)
当你调用$ http.post()时,它是异步的(不立即调用成功回调)并继续执行:system_connect()中没有更多代码,因此它返回到signin函数()。 此时,$ scope.formData仍为空。
然后,当你得到帖子的响应时,调用成功回调,$ scope.formData被修改但是signin函数()已经被执行了!
要解决此问题,您必须使用承诺:
public static void main(String[] args) {
DefaultClass def = new DefaultClass();
try{
def.setRating("ds");
}catch(Exception e){
System.out.println("hEY I GOT AN ERROR");
}
System.out.println(def.rating);
}
String rating;
public void setRating(String rating){
switch (rating){
case "G":
case "PG":
case "R":
case "NR": this.rating = rating;
break;
default: throw new IllegalArgumentException();
}
}
注意强>
$ http.post返回在登录功能中使用的承诺。