AngularJS控制器错误 - TypeError:undefined不是函数

时间:2014-10-08 01:15:10

标签: json angularjs

我正在尝试从json加载数据。但我的代码是错误的。我该如何解决?

错误消息

TypeError: undefined is not a function
    at new <anonymous> (http://s.codepen.io/boomerang/f0c70f5befd13788970cee6187a271461412729151639/index.html:66:15)
    at invoke (http://code.ionicframework.com/1.0.0-beta.6/js/ionic.bundle.js:12639:17)
    at Object.instantiate (http://code.ionicframework.com/1.0.0-beta.6/js/ionic.bundle.js:12650:23)
    at http://code.ionicframework.com/1.0.0-beta.6/js/ionic.bundle.js:15709:28
    at http://code.ionicframework.com/1.0.0-beta.6/js/ionic.bundle.js:15118:34
    at forEach (http://code.ionicframework.com/1.0.0-beta.6/js/ionic.bundle.js:9239:20)
    at nodeLinkFn (http://code.ionicframework.com/1.0.0-beta.6/js/ionic.bundle.js:15105:11)
    at compositeLinkFn (http://code.ionicframework.com/1.0.0-beta.6/js/ionic.bundle.js:14569:15)
    at compositeLinkFn (http://code.ionicframework.com/1.0.0-beta.6/js/ionic.bundle.js:14572:13)
    at publicLinkFn (http://code.ionicframework.com/1.0.0-beta.6/js/ionic.bundle.js:14474:30) 

JS档案

angular.module('ionicApp', ['ionic'])

.factory('JsonService', function($http) {

  var url="http://www.garsoncepte.com/json.php?callback=JSON_CALLBACK";

  var getData = function(successFn, errorFn){
    $http.get('url')
    .success(successFn)
    .error(errorFn);
  };

  return getData;
 })

.controller('MyCtrl', function($scope, JsonService) {
  JsonService.getData(function(data){ //It may be error code
    $scope.items = data;
  },function(error){});

});

我用这个:http://codepen.io/m_abdelfattah/pen/Gkiju

1 个答案:

答案 0 :(得分:0)

请改为尝试:

angular.module('ionicApp', [])
.factory('JsonService', function($http) {
  var url="http://www.garsoncepte.com/json.php?callback=JSON_CALLBACK";
  var getData = function(){
    return $http.get(url);
  };
  return {getData: getData};
 })

.controller('MyCtrl', function($scope, JsonService) {
  JsonService.getData()
  .success(function(data){
    $scope.items = data;
  })
  .error(function(){});
});

Example