在离子应用程序中从服务器调用数据

时间:2017-07-11 10:20:26

标签: angularjs json ionic-framework

Service Code

在我的Ionic应用程序中,我能够从.factory提供我的数据但是我想从服务器(JSON)获取数据,我已经尝试了很多代码但是无法解决问题。请建议我在services.js文件中写一个代码,以便我可以从http://localhost:3000/statments获取我的数据。

查看上面的服务代码,了解我如何从services.js

获取数据

1 个答案:

答案 0 :(得分:0)

//controller  
 app.controller('YourControllerName', YourControllerName);
 function YourControllerName($scope, myService) {
    $scope.fetchData = function() {
       myService.getData().then(function(result) {
         console.log(result.data);
       });
    }
 };

 //service.js
 app.service("myService", function ($http) {
     this.getData = function() {
        return $http.get('http://localhost:3000/');
     }
 });