2017-05-09 9 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; 
}; 
}); 

私はこのような角度コントローラに機能を呼び出すとき:

enter image description here

:私は、コンソールでこのような何かを取得しています

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

私はarr [0]を呼び出すと定義されません。 このデータはどのように入手できますか?

答えて

1

約束を返す必要があります。現在は、配列が更​​新される前に返されています。非同期は、まずすべての同期コードを実行してから、非同期のものを実行します。

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

約束を返すには、コードを変更する必要があります。しかし、これはあなたの呼び出しコードが非同期コードを処理しなければならないことを意味します。

あなたが

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; 
     }); 
}; 
}); 

と呼び出し元の関数の上に与えたコードの文脈では

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

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

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

私は 'angular.jsを取得しAMG:12520例外TypeError: – 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

が同一/同等のパラメータを作る'取得しています関数の呼び出しからundefined'の「を」プロパティを読み取ることができません。 getCommunication –

+0

「TypeError:コールバックが関数ではありません」というエラーが表示される – oded

関連する問題