[英]angular.foreach function throws syntax error
function getTotal.getValues() is a server call that returns "one", "two", "three" ... "nine". 函数getTotal.getValues()是一个服务器调用,返回“一个”,“两个”,“三个” ...“九”。 i can print them with console.log(res).
我可以使用console.log(res)打印它们。 however, it seems i cannot push them into variable v created inside runTest Function.
但是,似乎我无法将它们推入runTest Function内部创建的变量v中。 in this code, console.log(r) does not print anything because return v is empty.
在此代码中,console.log(r)不输出任何内容,因为return v为空。 any ideas?
有任何想法吗?
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);
});
Are you mistaken between angular forEach and javascript forEach . 您是否在angular forEach和javascript forEach之间误解了。
Angular forEach should be like this. Angular forEach应该是这样的。 I can't see angular forEach I your code snippet.
我看不到for your我的代码片段。
angular.forEach(test, function(value, key) {
console.log(value);
});
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! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.