2017-05-09 65 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

相關問題