Angularjs编辑阵列从HTTP GET URL

问题描述:

我有这样的阵列,我通过以下方法得到恢复。并通过http post url方法将其发回到另一个网址。我卡住的是异步调用的概念,因此我无法检索最喜欢的数字列表以供编辑。请帮忙!Angularjs编辑阵列从HTTP GET URL

我试图用承诺的方法如下:

 app.factory('myService', function($http) { 
     var myService = { 
     async: function(url) { 
     var promise = $http.get(url).then(function (response) { 
      console.log(response); 
      return response.data; 
     }); 
     // Return the promise to the controller 
     return promise; 
    } 
}; 

    return myService; 
}); 

在我的控制器中我做:

angular.module('JuryApp').controller('mycontroller', ['myService', function (myService) { 

     myService.async(url).then(function(d) { 
    $scope.data = d; 
}); 

    app.controller('MainCtrl', function(myService,$scope) { 
    // Call the async method and then do stuff with what is returned inside  our own then function 
    myService.async().then(function(d) { 
    $scope.data = d; 
    }); 
}); 

但我不断收到错误“d没有定义”。它不断给出某种错误,调试器进入一个无限循环或某处。

我觉得你太过于复杂了。异步调用实际上是非常简单的:

你服务:

app.factory("myService", ["$http", function($http) { 
    var MyService = { 
    getData: function(url) { 
     return $http.get(url); //$http returns a promise by default 
    } 
    }; 
    return MyService; 
})]; 

你的控制器:

angular.module('JuryApp').controller('mycontroller', ['myService', function (myService) { 
    $scope.FavNumbers = []; 
    var url = "http://my.api.com/"; 
    myService.getData(url).then(function(response) { 
    $scope.FavNumbers = response.data[0].FavNumbers; 
    }); 
}]); 

这就是所有你需要做的。