17
votes

I'm developing a mobile app with Cordova and Angular JS. My app is on an easyphp localhost. I use Angular Routes with a ng view directive in my index.html. And I've got this :

TypeError: Cannot read property 'get' of undefined

angular.module('app.controllers', [])
.controller('MainCtrl', ['$scope', function ($scope) {
    $scope.status = "Accueil";
}])
.controller('ViewCtrl', ['$scope', function ($scope, $http) {
    $http.get('mobile.php/getAnnonces/limit=10')
        .success(function(data, status, headers, config) {
          $scope.posts = data;
        })
        .error(function(data, status, headers, config) {
          // log error
    });
}])
...

If I test the URL my script returns JSON (with json_encode). Where am I wrong ?

Thanks for your help,

Regards

1
in ViewCtrl, you forgot to pass the depencendy $http as a string before passing it as an argument - Raghavendra

1 Answers

37
votes

try to change

angular.module('app.controllers', [])
.controller('MainCtrl', ['$scope', function ($scope) {
    $scope.status = "Accueil";
}])
.controller('ViewCtrl', ['$scope','$http', function ($scope, $http) {
    $http.get('mobile.php/getAnnonces/limit=10')
        .success(function(data, status, headers, config) {
          $scope.posts = data;
        })
        .error(function(data, status, headers, config) {
          // log error
    });
}]);

Changes: Passing $http service to controller as string so that will resolve at runtime.