如何在 Angularjs 中的 init 函数中访问 .then 中设置的值?
How to access the value set in .then in my init function in Angularjs?
如何在 $scope.init() 中访问 $scope.email。目前它给我未定义。
app.controller('someCtrl',
[$scope,$http,$location,function($scope,$http,$location){
$scope.init = function(){
$scope.func1();
console.log($scope.email); // giving undefined
}
$scope.func1 = function(){
var data = {
'Content-Tye' : 'application/json',
'some-token' : $cookies.get('sometoken')
};
$http({
method : 'GET',
url : some url,
headers = data,
timeout: 50000
}).then(function(successData){
$scope.email = successData.entity.email;
}).then($http({
method:'GET',
url: some url,
headers: data,
timeout:50000
})
.then ..)
}
}])
我有一系列的 .then 让我很困惑。如何等到我的 $scope.email 设置好?我的 console.log 语句在设置之前被执行。我如何让它等待?我确实读过关于 promise 的内容,但让我感到困惑的是多个 .then 语句。我想等到 first.then 完成后做控制台日志。
如果您想等待,则 func1 需要 return 一个承诺,然后您需要使用该承诺的 .then
函数
$scope.init = function(){
$scope.func1()
.then(function() { // <--- using .then
console.log($scope.email);
});
}
$scope.func1 = function(){
var data = {
'Content-Tye' : 'application/json',
'some-token' : $cookies.get('sometoken')
};
return $http({ // <--- notice the return
method : 'GET',
url : some url,
headers = data,
timeout: 50000
}).then(function(successData){
$scope.email = successData.entity.email;
}).then(
... etc ...
);
});
此外,如果您的任何 .then
函数是异步的并且您也想等待它们,请确保它们 return 他们的承诺。
尼古拉斯勉强抢在我前面,他的回答会奏效,但如果你不关心其他 .then 的问题,只想确保电子邮件已设置,那么这对你有用。
app.controller('someCtrl',
[$scope,$http,$location,function($scope,$http,$location){
$scope.init = function(){
$scope.func1().then(function() {
console.log($scope.email); // giving undefined
});
}
$scope.func1 = function(){
var deferred = $q.defer();
var data = {
'Content-Tye' : 'application/json',
'some-token' : $cookies.get('sometoken')
};
$http({
method : 'GET',
url : some url,
headers = data,
timeout: 50000
}).then(function(successData){
$scope.email = successData.entity.email;
deferred.resolve();
}).then($http({
method:'GET',
url: some url,
headers: data,
timeout:50000
})
.then ..)
}
return deferred.promise;
}])
你可以试试下面的代码,
app.controller('someCtrl', [$scope,$http,$location,function($scope,$http,$location){
$scope.init = function(){
var data = {
'Content-Tye' : 'application/json',
'some-token' : $cookies.get('sometoken')
};
getEmail(data).then(function(response){
$scope.email = response.data;
console.log($scope.email);
$scope.func1(data);
});
}
function getEmail(data){
return $http({
method : 'GET',
url : some url,
headers = data,
timeout: 50000
}).then(function(response){
return response.data.email;
});
}
$scope.func1 = function(data){
$http({
method : 'GET',
url : some url,
headers = data,
timeout: 50000
}).then(function(response){
$http({
method:'GET',
url: some url,
headers: data,
timeout:50000
}).then(function(response){
// do something...
});
};
}]);
如何在 $scope.init() 中访问 $scope.email。目前它给我未定义。
app.controller('someCtrl',
[$scope,$http,$location,function($scope,$http,$location){
$scope.init = function(){
$scope.func1();
console.log($scope.email); // giving undefined
}
$scope.func1 = function(){
var data = {
'Content-Tye' : 'application/json',
'some-token' : $cookies.get('sometoken')
};
$http({
method : 'GET',
url : some url,
headers = data,
timeout: 50000
}).then(function(successData){
$scope.email = successData.entity.email;
}).then($http({
method:'GET',
url: some url,
headers: data,
timeout:50000
})
.then ..)
}
}])
我有一系列的 .then 让我很困惑。如何等到我的 $scope.email 设置好?我的 console.log 语句在设置之前被执行。我如何让它等待?我确实读过关于 promise 的内容,但让我感到困惑的是多个 .then 语句。我想等到 first.then 完成后做控制台日志。
如果您想等待,则 func1 需要 return 一个承诺,然后您需要使用该承诺的 .then
函数
$scope.init = function(){
$scope.func1()
.then(function() { // <--- using .then
console.log($scope.email);
});
}
$scope.func1 = function(){
var data = {
'Content-Tye' : 'application/json',
'some-token' : $cookies.get('sometoken')
};
return $http({ // <--- notice the return
method : 'GET',
url : some url,
headers = data,
timeout: 50000
}).then(function(successData){
$scope.email = successData.entity.email;
}).then(
... etc ...
);
});
此外,如果您的任何 .then
函数是异步的并且您也想等待它们,请确保它们 return 他们的承诺。
尼古拉斯勉强抢在我前面,他的回答会奏效,但如果你不关心其他 .then 的问题,只想确保电子邮件已设置,那么这对你有用。
app.controller('someCtrl',
[$scope,$http,$location,function($scope,$http,$location){
$scope.init = function(){
$scope.func1().then(function() {
console.log($scope.email); // giving undefined
});
}
$scope.func1 = function(){
var deferred = $q.defer();
var data = {
'Content-Tye' : 'application/json',
'some-token' : $cookies.get('sometoken')
};
$http({
method : 'GET',
url : some url,
headers = data,
timeout: 50000
}).then(function(successData){
$scope.email = successData.entity.email;
deferred.resolve();
}).then($http({
method:'GET',
url: some url,
headers: data,
timeout:50000
})
.then ..)
}
return deferred.promise;
}])
你可以试试下面的代码,
app.controller('someCtrl', [$scope,$http,$location,function($scope,$http,$location){
$scope.init = function(){
var data = {
'Content-Tye' : 'application/json',
'some-token' : $cookies.get('sometoken')
};
getEmail(data).then(function(response){
$scope.email = response.data;
console.log($scope.email);
$scope.func1(data);
});
}
function getEmail(data){
return $http({
method : 'GET',
url : some url,
headers = data,
timeout: 50000
}).then(function(response){
return response.data.email;
});
}
$scope.func1 = function(data){
$http({
method : 'GET',
url : some url,
headers = data,
timeout: 50000
}).then(function(response){
$http({
method:'GET',
url: some url,
headers: data,
timeout:50000
}).then(function(response){
// do something...
});
};
}]);