Failed to perform a request with Angular.js $http is not defined

Asked

Viewed 315 times

0

I’m starting to use angular.js, I’m studying one. Tutorial (Definitive Guide to Learning Angularjs in One Day), but at first I had problems with the $http request. I’m getting the message: $http is not defined

In the index header I include:

<script src="http://code.angularjs.org/1.0.1/angular-1.0.1.min.js"></script>

My HTML:

{% verbatim %}
    <ul class="dropdown-menu" ng-controller="sbMenuTopSupport">
        <li ><a href="{{support.action.uri}}">{{support.action.label}}</a></li>
        <li class="divider"></li>
        <li>

        </li>
    </ul>
{% endverbatim %}

My javascript:

<script type="text/javascript">
    var sbMenuTop = angular.module('sbMenuTop', []);
    sbMenuTop.controller('sbMenuTopSupport', ['$scope', function ($scope) {
            $scope.support = {};
            $scope.support.action = {
                "uri": "/SBSystem/web/app_dev.php/support/status.json",
                "label": "Unavailable"
            };
            $scope.support.access = {
                "ipaddress": null,
                "username": null,
                "password": null
            };

            $http({
                method: 'GET',
                url: $scope.support.action.uri
            }).sucess(function (data, status, headers, config) {
                $scope.support = data;
                //recuperação de dados bem sucedida
            }).error(function (data, status, headers, config) {
                //alguma erro ocorreu :(
            });
        }]);
</script>

These are the error messages I get:

 ReferenceError: $http is not defined
    at new <anonymous> ((index):128)
    at d (angular-1.0.1.min.js:27)
    at Object.instantiate (angular-1.0.1.min.js:27)
    at $get (angular-1.0.1.min.js:50)
    at angular-1.0.1.min.js:42
    at m (angular-1.0.1.min.js:6)
    at k (angular-1.0.1.min.js:42)
    at e (angular-1.0.1.min.js:38)
    at e (angular-1.0.1.min.js:38)
    at e (angular-1.0.1.min.js:38)

1 answer

1


You did not set the service as a controller dependency $http.

The controller definition should look like this:

sbMenuTop.controller('sbMenuTopSupport', ['$scope', '$http', function ($scope, $http) {
  • The tutorial I got is really not very cool, besides missing define the dependency in the constructor the Promise is with wrong syntax: sucess where should be Success.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.