如何在javascript中获取数组格式?
我得到一个带有工厂函数的AngularJs数组。这是控制台如何在javascript中获取数组格式?,javascript,angularjs,loopbackjs,Javascript,Angularjs,Loopbackjs,我得到一个带有工厂函数的AngularJs数组。这是控制台 Array[0] 0: "value1" 1: "value2" length:2 但是当我想得到数组的长度时 console.log(array.length) 在环回中从mysql获取数据 app.factory("getFoo", function(Communications){ return { getCommi: function(val,id){ var array = []; v
Array[0]
0: "value1"
1: "value2"
length:2
但是当我想得到数组的长度时
console.log(array.length)
在环回中从mysql获取数据
app.factory("getFoo", function(Communications){
return {
getCommi: function(val,id){
var array = [];
var myVals = Communications.find({
filter: {
where: {
and : [{
communications_type_code : val
},{
object_id : id
}]
}
}
}, function(data){
for(var i=0; i< data.length; i++){
array[i] = data[i].contact_value;
}
return array;
});
return array;
}
}
});
app.factory(“getFoo”,功能(通信){
返回{
getCommi:函数(val,id){
var数组=[];
var myVals=Communications.find({
过滤器:{
其中:{
以及:[{
通信类型代码:val
},{
对象id:id
}]
}
}
},函数(数据){
对于(变量i=0;i
控制器看起来像:
app.controller('usersFormCtrl', ['$scope','getFoo',function($scope,getFoo){
var emails = getFoo.getCommi(3,1);
setTimeout(function(){
$scope.formModel.emails = [];
for(var index=0; index < $scope.emails.length; index++){
$scope.emails = emails;
}
}, 0)
}])
app.controller('usersFormCtrl',['$scope','getFoo',函数($scope,getFoo){
var=getFoo.getCommi(3,1);
setTimeout(函数(){
$scope.formModel.emails=[];
对于(var索引=0;索引<$scope.emails.length;索引++){
$scope.emails=电子邮件;
}
}, 0)
}])
我知道长度是0。为什么会这样?这是一个时间问题。第一次询问长度时,它确实为0,但几秒钟后使用Chrome Dev工具检查对象时,您正在检查已填充的活动对象 您可以使用
setTimeout
setTimeout(function(){
console.log(array);
}, 0)
您可以检查此项以获得进一步解释
更新
在angular中,使用$timeout
而不是setTimeout
。像这样:
$timeout(function(){
console.log(array);
}, 0)
JavaScript中数组的长度属性是不可变的。您可以通过定义数组中的属性数量来设置数组的大小:var a=new Array(2),或者只传入值:var a=['value1','value2']。试试这个
console.log(array[0].length)
这里混合了异步和同步方法
Communications.find
是异步的,但是您可以在getCommi
中像同步函数一样使用它
调用getCommi
时,函数立即返回空数组
请根据以下内容进行更改
app.factory("getFoo", function(Communications){
return {
getCommi: function(val,id, cb){
var array = [];
var myVals = Communications.find({
filter: {
where: {
and : [{
communications_type_code : val
},{
object_id : id
}]
}
}
}, function(data){
for(var i=0; i< data.length; i++){
array[i] = data[i].contact_value;
}
cb(null, array);
});
}
}
});
app.factory(“getFoo”,功能(通信){
返回{
getCommi:函数(val、id、cb){
var数组=[];
var myVals=Communications.find({
过滤器:{
其中:{
以及:[{
通信类型代码:val
},{
对象id:id
}]
}
}
},函数(数据){
对于(变量i=0;i
及
app.controller('usersFormCtrl',['$scope','getFoo',函数($scope,getFoo){
getFoo.getCommi(3,1,函数(错误,电子邮件){
$scope.formModel.emails=[];
对于(var索引=0;索引<$scope.emails.length;索引++){
$scope.emails=电子邮件;
}
});
}])
免责声明:我不知道。请分享更多您的代码。您在控制台的何处登录以及从何处获取阵列?如何向阵列添加元素?请分享。您的显示代码是正确的,我认为问题在于您正在将元素添加到数组中。检查此项:如果长度返回0
,则数组[0]。长度将抛出错误<代码>未定义的长度
您可以添加与此相关的javascript代码吗。但因为我用的是角度,我用的是范围。我更新了我的问题。@oded:我更新了我的答案。请让我知道为什么我的错误答案是否定的。发生意外。
app.controller('usersFormCtrl', ['$scope','getFoo',function($scope,getFoo){
getFoo.getCommi(3,1, function(err, emails){
$scope.formModel.emails = [];
for(var index=0; index < $scope.emails.length; index++){
$scope.emails = emails;
}
});
}])