kAldown
kAldown

Reputation: 626

Angularjs Error: [$injector:modulerr] 'ngRoute'

Got an error trying to reproduce angularjs example

    <!DOCTYPE html>

    <html ng-app="lk">
        <head> 
            <meta charset="UTF-8">
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-route.js"></script>
            <script type="text/javascript" src="lk.js"></script>
        </head>
        <body ng-controller="apiCtrl"> 
            <div> 
                {{ data }}
                {{ status }}
                <br>
                <button ng-click="fetch()">fetch</button>
                <br>    
                <input type="text" ng-model="text"></input>
                {{ text }}
            </div>
        </body>
    </html>

# lk.js
    (function(){
        var app = angular.module("lk", ["ngRoute"]);
        app.controller("apiCtrl", ["$http", "$scope", function($scope, $http){ 
            $scope.method = "POST";
            $scope.url = "https://example.com/api";

            $scope.fetch = function() {
                $scope.response = null;

                $http({method: $scope.method, url: $scope.url}).
                    then(function(response){
                        $scope.status = response.status;
                        $scope.data = response.data;
                    }, function(response){
                        $scope.data = response.data || "Request failed";
                        $scope.status = response.status;
                });
            };
        }]);
    });

Error: Error: [$injector:modulerr]

Tried to move ng-app and ng-controller derectives between tags.

Tried to change min.js to full angular.

Following this, I added dependency according this manual.

Still no solution.

Upvotes: 0

Views: 425

Answers (1)

RIYAJ KHAN
RIYAJ KHAN

Reputation: 15292

Change this one.

app.controller("apiCtrl", ["$http", "$scope", function($http, $scope){

Order is important when you injecting the service

Upvotes: 1

Related Questions