function getTotal.getValues() is a server call that returns "one", "two", "three" ... "nine". i can print them with console.log(res). however, it seems i cannot push them into variable v created inside runTest Function. in this code, console.log(r) does not print anything because return v is empty. any ideas?
function getTotal.getValues()是一个服务器调用,返回“one”,“two”,“three”......“nine”。我可以用console.log(res)打印它们。但是,似乎我无法将它们推入runTest函数内创建的变量v中。在此代码中,console.log(r)不会打印任何内容,因为return v为空。有任何想法吗?
var test = [1,2,3,4,5,6,7,8,9];
function runTest(val) {
var v = [];
val.forEach(function(t) {
getTotal.getValues(t).then(function(res) {
//console.log(res);
v.push(res);
});
});
return v;
}
runTest(test).forEach(function(r) {
console.log(r);
});
2 个解决方案
#1
1
Are you mistaken between angular forEach and javascript forEach.
你是否在angular forEach和javascript forEach之间弄错了。
Angular forEach should be like this. I can't see angular forEach I your code snippet.
Angular forEach应该是这样的。我看不到有角度的每个我的代码片段。
angular.forEach(test, function(value, key) {
console.log(value);
});
#2
0
This is how your forEach function should look like:
这就是你的forEach函数应该是这样的:
var test = [1,2,3,4,5,6,7,8,9];
test.forEach(function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element);
});
// logs:
// a[0] = 1
// a[1] = 2
// a[2] = 3
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
But if you want to use angular.forEach(), refer to: https://docs.angularjs.org/api/ng/function/angular.forEach
但是如果你想使用angular.forEach(),请参考:https://docs.angularjs.org/api/ng/function/angular.forEach
Good luck!
祝你好运!
#1
1
Are you mistaken between angular forEach and javascript forEach.
你是否在angular forEach和javascript forEach之间弄错了。
Angular forEach should be like this. I can't see angular forEach I your code snippet.
Angular forEach应该是这样的。我看不到有角度的每个我的代码片段。
angular.forEach(test, function(value, key) {
console.log(value);
});
#2
0
This is how your forEach function should look like:
这就是你的forEach函数应该是这样的:
var test = [1,2,3,4,5,6,7,8,9];
test.forEach(function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element);
});
// logs:
// a[0] = 1
// a[1] = 2
// a[2] = 3
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
But if you want to use angular.forEach(), refer to: https://docs.angularjs.org/api/ng/function/angular.forEach
但是如果你想使用angular.forEach(),请参考:https://docs.angularjs.org/api/ng/function/angular.forEach
Good luck!
祝你好运!