Javascript 如何访问中设置的值。然后在Angularjs中的my init函数中?
如何访问$scope.init()中的$scope.email。目前它给我的是未定义的Javascript 如何访问中设置的值。然后在Angularjs中的my init函数中?,javascript,angularjs,angular-promise,Javascript,Angularjs,Angular Promise,如何访问$scope.init()中的$scope.email。目前它给我的是未定义的 app.controller('someCtrl', [$scope,$http,$location,function($scope,$http,$location){ $scope.init = function(){ $scope.func1(); console.log($scope.email); // giving
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 ..)
}
}])
我有一系列的问题,这让我很困惑。如何等待设置$scope.email?My console.log语句在设置之前执行。我如何让它等待?我确实读过关于承诺的书,但让我困惑的是,我有多重陈述。我想先等待。然后完成控制台日志。如果您想等待,则func1需要返回一个承诺,然后您需要使用该承诺的
。然后函数
$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 ...
);
});
$scope.init=function(){
$scope.func1()
.then(function(){/Nicholas勉强击败了我,他的答案会有效,但如果你不关心另一个。then’s,只是想确保电子邮件已经设置好,那么这将对你有效
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...
});
};
}]);
谢谢尼古拉斯的回答。这回答了我的问题。