2017-05-09 118 views
2

我想知道如何从角函数检索“好”数组。我有这个功能的角度:如何从环回中的AngularJS回调函数获取数据?

app.run(function($rootScope,Communications,$http,$filter) { 

$rootScope.getCommunication = 
function(object_type,val,id,isType,isSendSms,getNotes){ 

    var array = {}; 
    var newArr = []; 

    // getting data from mysql data 
    var myVals = Communications.find({ 
     filter: { 
      where: { 
      and : [{ 
       communications_type_code : val 
      },{ 
       object_id : id 
      },{ 
       object_type : object_type 
      }] 
      } 
     } 
     }).$promise 
     .then(function(data) { 

      for(var ind=0; ind<data.length; ind++){ 
      array['address_type'] = data[ind].address_type; 
      array['contact_value'] = data[ind].contact_value; 
      array['send_sms'] = data[ind].send_sms; 
      } 

      newArr.push(array); 
      return newArr; 
     }); 
    return newArr; 
}; 
}); 

当我打电话给函数在角控制器是这样的:

var arr = $rootScope.getCommunication(2,3,$id); 
console.log(arr); 

我收到的控制台是这样的:

enter image description here

当我调用arr [0]时,我得到未定义。 我怎样才能收到这些数据?

回答

1

您需要返回承诺,目前您在更新数组之前返回数组。异步通过先运行所有同步代码然后运行任何异步工作。

var arr = []; // runs first 
promise.then(function(result){arr = result}) // runs third. 
return arr; // runs second 

您需要更改代码以返回承诺。然而这也意味着你的调用代码必须处理异步代码。

function asyncFunc() { 
return promise.then(function(result){return result}); 
} 

asyncFunc().then(function(result) {console.log(result)}) // output is the value of result. 

在你上面

app.run(function($rootScope,Communications,$http,$filter) { 

$rootScope.getCommunication = 
function(object_type,val,id,isType,isSendSms,getNotes){ 
    // getting data from mysql data 
    return Communications.find({ 
     filter: { 
      where: { 
      and : [{ 
       communications_type_code : val 
      },{ 
       object_id : id 
      },{ 
       object_type : object_type 
      }] 
      } 
     } 
     }).$promise 
     .then(function(data) { 
      var array = {}; 
      var newArray = []; 
      for(var ind=0; ind<data.length; ind++){ 
      array['address_type'] = data[ind].address_type; 
      array['contact_value'] = data[ind].contact_value; 
      array['send_sms'] = data[ind].send_sms; 
      } 

      newArr.push(array); 
      return newArr; 
     }); 
}; 
}); 

了代码,并调用函数的上下文:

var arr = $rootScope.getCommunication(2,3,$id) 
        .then(function(arr){console.log(arr)}) 
+0

我AMG越来越'angular.js:12520类型错误:无法从函数的调用读undefined'的属性“然后” – oded

0

您可以按以下方式

app.run(function($rootScope,Communications,$http,$filter) { 

$rootScope.getCommunication = 
function(object_type,val,id,isType,isSendSms,getNotes, callback){ 

    var array = {}; 
    var newArr = []; 

    // getting data from mysql data 
    var myVals = Communications.find({ 
     filter: { 
      where: { 
      and : [{ 
       communications_type_code : val 
      },{ 
       object_id : id 
      },{ 
       object_type : object_type 
      }] 
      } 
     } 
     }).$promise 
     .then(function(data) { 

      for(var ind=0; ind<data.length; ind++){ 
      array['address_type'] = data[ind].address_type; 
      array['contact_value'] = data[ind].contact_value; 
      array['send_sms'] = data[ind].send_sms; 
      } 

      newArr.push(array); 

      //return using callback 
      return callback(newArr); 

     }); 

    //return using callback 
    return callback(newArr); 

}; 
}); 

使用回调使用回调来访问res ULT

$rootScope.getCommunication(2,3,$id, function(result){ 
    var arr = result 
}) 
+0

我得到'不确定callback' – oded

+0

做出相同/相等的参数调用函数时getCommunication –

+0

我仍然得到一个错误'TypeError:回调不是一个函数' – oded