2014-01-09 54 views
0

我在一個angularjs模塊中有一個工作的JSONP調用來獲得工作我在模塊外部定義了回調(見下文)。我無法獲得模塊內部的回調(callback未定義)。如何在angularjs裏處理JSONP的回調

如何在角度模塊內獲得jsonp_callback(data)

function jsonp_callback(data) { 
// returning from async callbacks is (generally) meaningless 
alert('good'); 
console.log(data.found); 
} 


angular.module('me.services', []) 
.factory('Products', ['$http', '$q', '$filter', function ($http, $q, $filter) { // Query API and return JSON. 
    return { 
     get: function() { 
      var _deferred = $q.defer(), 
       funds = [], 
       products = []; 

      var murl = "http://localhost:82/api/productlist/getitems"; 
      $http.jsonp(murl + "?callback=jsonp_callback", null, function (result) { 
       alert('ts'); 
      }); 
     } 

回答

0

根據您想如何處理內部角數據:

function jsonp_callback(data) { 

    // for binding to a service property 
    angular.element(document).injector().get('ServiceName').whatever = data 

    // for binding to a controller scope propery 
    angular.element('#someDiv').scope().whatever = data 

    ...