0

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>

2
  • you have to put it in something that can be injected with it. Commented Jan 21, 2018 at 0:54
  • 1
    You realize you have likeCTRL defined twice in the code you've presented here, right? And in both definitions, you're not using $http within the function? Commented Jan 21, 2018 at 0:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.