Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'get' of undefined with Angular

Tags:

angularjs

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

like image 851
T-prod Avatar asked Jun 29 '14 17:06

T-prod


1 Answers

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.

like image 121
Ravi Mittal Avatar answered Sep 20 '22 15:09

Ravi Mittal