Vzupo
Vzupo

Reputation: 1468

angularjs $http not defined but it is

I am simply trying to log something to the console, but it keeps saying $http is not defined. Can someone point out what i am doing wrong? It doesnt give error when its placed inside of the likeCtrl function, but I dont want it there but to sit outside of it.

angular.module('app', ['ui.bootstrap']);
        angular.module('app').controller('likeCTRL', likeCTRL);

        function likeCTRL($rootScope, $scope, $uibModal, $http) {

  
        
        }
            
        var domain = '';

            var __REQUESTDIGEST = '';
            var app = angular.module('app', ['ui.bootstrap']);

            var User = $http.get(domain + "/_api/web/currentuser", {
                headers: {
                    "Accept": "application/json; odata=verbose"
                }
            });
            

            
            
            function likeCTRL($rootScope, $scope, $uibModal, $http) {

                User.then(function(response) {
                    console.log(response.data.d.Title);
                    $scope.myName = response.data.d.Title;

                    if ($scope.myName == "Doe, Jane") {
                        alert("this");
                  
                    }


                });
            }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body style="background-color:whitesmoke" ng-app="app" ng-controller="likeCTRL">
     
        <div class="container">
            <div class="row">

                
                        <a id="admin" class="pull-right" href="" target="_blank" style="display: none">| Admin | |</a>

                  </div>

        </div>






    </body>

Upvotes: 0

Views: 56

Answers (1)

Estus Flask
Estus Flask

Reputation: 222319

The error is caused by the fact that some controller code doesn't reside in controller function and is evaluated outside AngularJS application. So no, $http is not defined there, JS errors can be trusted.

likeCTRL function is being redefined. There should be only one likeCTRL function.

This code

    var domain = '';

        var __REQUESTDIGEST = '';
        // var app = angular.module('app', ['ui.bootstrap']);
        var User = $http.get(domain + "/_api/web/currentuser", {
            headers: {
                "Accept": "application/json; odata=verbose"
            }
        });

should reside inside controller function.

Notice that var app = angular.module('app', ['ui.bootstrap']) was commented. angular.module('app', ['ui.bootstrap']) redefines existing module and will cause problems in future. It shouldn't be there.

Upvotes: 1

Related Questions