AngularJS module.factory获取远程json

时间:2015-11-22 15:23:30

标签: json angularjs cordova

我使用AngularJS处理PhoneGap我需要一些帮助才能从远程JSON服务器获取数据。我怎么能在模块工厂做到这一点?

我的JSON文件:

[
{
'name':'',
'number:'',
'department':''
}
]

我的JavaScript:

(function(){
    use strict';
          var module = angular.module('app', ['onsen']);

          module.controller('AppController', function($scope, $data) {
            $scope.doSomething = function() {
              setTimeout(function() {


   ons.notification.alert({ message: 'tapped' });
          }, 100);
        };
      });



module.controller('DetailController', function($scope, $data) {
    $scope.item = $data.selectedItem;
  });

  module.controller('MasterController', function($scope, $data) {
    $scope.items = $data.items;

    $scope.showDetail = function(index) {
      var selectedItem = $data.items[index];
      $data.selectedItem = selectedItem;
      $scope.navi.pushPage('detail.html', {title : selectedItem.title});
    };
  });

  module.factory('$data', function() {
      var data = {};

      data.items = [
          {
              title: 'Noor almosswi',
              label: '',
              desc: 'Working in Managment',
              phone: '0000',
              photo:'http://cdn2.hubspot.net/hub/63072/file-14997515-jpg/images/jeremywilcomb_thedanielgroup.jpg?t=1430422772606&width=229&height=229'
          },
          {
              title: 'Safe khalid',
              label: '',
              desc: 'Working in accounting',
              phone: '00000',
              photo:'http://localhost/workflow/photos/c0d179d424ae9c327609c5d80e94c6e0_thumb.jpg'
          },
          {
              title: 'Yussif ali',
              label: '',
              desc: 'Working in Development',
              phone: '0000',
              photo:'http://www.beyondcareersuccess.com/wp-content/uploads/2012/08/employee.png'
          },
          {
              title: 'Ali kreeam',
              label: '',
              desc: 'Working in call center',
              phone: '000000',
              photo:'http://www.piranhaphotography.com/blog/wp-content/uploads/2010/05/brookfield-employee-portrait-photograph-0366.jpg'
          }
      ];

      return data;
  });
})();

1 个答案:

答案 0 :(得分:0)

在您的工厂中,您可以使用

$http.get('url').then(function(data){});

确保你也注入$ http