Javascript AngularJS:从函数返回数据并将其分配给变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38867933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS: returning data from a function and assigning it to a variable
提问by Raghav
I am new to angularjs, and I am trying to return the data from a function to another function, and store it in a variable.
我是 angularjs 的新手,我试图将数据从一个函数返回到另一个函数,并将其存储在一个变量中。
$scope.myNameValidate = function(name){
$scope.friendsList = $scope.getAllFriends(name);
console.log("data", $scope.friendsList);
}
$scope.getAllFriends = function(name){
friendService.getAllfriends(name)
.then(function(data){
//success
console.log(data);
}, function(err){
//error
})
}
I want to store all the objects in a variable but I get undefined as result.
我想将所有对象存储在一个变量中,但结果未定义。
output in console
控制台输出
data undefined
数据未定义
[Object, Object, Object, Object, Object, Object]
[物体,物体,物体,物体,物体,物体]
回答by RIYAJ KHAN
You need to know the Angular promise.
您需要了解Angular 承诺。
This issue related to Asynchronousoperation.
此问题与异步操作有关。
You can fix it with proper then
able chaining.
You can do it in this way.
您可以使用适当的then
链接来修复它。你可以这样做。
$scope.myNameValidate = function(name) {
$scope.getAllFriends(name)
.then(function(data) {
$scope.friendsList = data;
console.log(data);
}, function(err) {
//error
});
}
$scope.getAllFriends = function(name) {
return friendService.getAllfriends(name)
}
回答by Jorawar Singh
Why its' undefined?
为什么它的'未定义?
Your function $scope.getAllFriends()
is not returning anything witch can set data to $scope.friendsList.
您的函数$scope.getAllFriends()
没有返回任何女巫可以将数据设置为 $scope.friendsList 的内容。
function myFunc(){
var i = 5;
}
var myVar = myFunc();
myVar will not have value 5.
function myFunc(){
var i = 5;
return i;
}
var myVar = myFunc();
In angular even if it's asynchronous data when you set to $scope as soon data has arrived angular will update your view and scope.
在 angular 中,即使它是异步数据,当您设置为 $scope 时,一旦数据到达,angular 也会更新您的视图和范围。
回答by Alexey Katayev
You cannot use async callbacks this way. In your particular case you should set $scope.friendsList inside the success callback.
您不能以这种方式使用异步回调。在您的特定情况下,您应该在成功回调中设置 $scope.friendsList 。
$scope.myNameValidate = function(name) {
$scope.getAllFriends(name);
}
$scope.getAllFriends = function(name) {
friendService.getAllfriends(name)
.then(function(data) {
$scope.friendsList = data;
console.log("data", $scope.friendsList);
}, function(err){
//error
})
}
回答by Akash Bhandwalkar
$scope.myNameValidate = function(name){
$scope.friendsList = $scope.getAllFriends(name)
.then(function(data){
//here is your data
})
.catch(error){
//Error
}
console.log("data", $scope.friendsList);
}
$scope.getAllFriends = function(name){
var promise = friendService.getAllfriends(name)
.then(function(data){
return data;
}, function(err){
//error
})
return promise;
}
This is asynchronus call thats why you got undefined
这是异步调用,这就是您未定义的原因