简体   繁体   中英

Error: [$injector:unpr] Unknown provider: $scope in angular Js when routing

var myWebsite = angular.module('myWebsite', ['ngRoute']);


myWebsite.config(['$scope', '$routeProvider', function($routeProvider) {
    $routeProvider
    .when("/home", {
        templateUrl : "index.html",
    })
    .when("/about", {
        templateUrl : "views/about-view.html",
    })
    .otherwise({
        redirectTo : "/home"
    });
}]);

myWebsite.controller('myWebsiteCtrl', ['$scope', function($scope){
    console.log("angular is initialised");
}]);

myWebsite.controller('aboutCtrl', ['$scope', function($scope){
    console.log("about controller is initialised");
}]);

This code is giving error Error: [$injector:unpr] Unknown provider: $scope. i am using these lines at header

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.js">`enter code here`</script>

<script src="https://code.angularjs.org/1.6.3/angular-route.js"></script>

$scope isn't injectable inside config block. You can avail $scope dependency injected on controller only. Technically it is not(never) possible to inject $scope in config phase, as config block will allow to inject providers only. additionally config phase happens at time of application bootstrap(specifically before run phase).

myWebsite.config(['$routeProvider', function($routeProvider) {

Note: Make sure you have angular-route.js referenced on page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM