2017-04-25 103 views
0

努力學習angluar航線結構,但我失敗了關於實施ngRoute角路線執行失敗

我的應用程序

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

配置

app.config(['$routeProvider', function($routeProvider) { 
    $routeProvider.otherwise({redirectTo: '/'}); 
}]); 

我的指令,使用路由器的定義

app.directive("applicationDirective", function($router) { 

    return { 
     restrict : "E", 
     templateUrl: 'app/templates/application.html', 
     controller: 'applicationController' 
    }; 

}); 

而我在HTML中的實現

<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.css"> 

     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.js"></script> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-animate.js"></script> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-aria.js"></script> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-messages.js"></script> 
     <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.js"></script> 

     <script src="app/app.js"></script> 

似乎沒問題;但不合格...與:

angular.js:14525 Error: [$injector:unpr] Unknown provider: $routerProvider <- $router <- applicationDirectiveDirective 
http://errors.angularjs.org/1.6.4/$injector/unpr?p0=%24routerProvider%20%3C-%20%24router%20%3C-%20applicationDirectiveDirective 
    at angular.js:66 
    at angular.js:4789 
    at Object.getService [as get] (angular.js:4944) 
    at angular.js:4794 
    at getService (angular.js:4944) 
    at injectionArgs (angular.js:4969) 
    at Object.invoke (angular.js:4995) 
    at angular.js:8110 
    at forEach (angular.js:403) 
    at Object.<anonymous> (angular.js:8108) 

有沒有人得到一個提示如何解決它。

問候 n00n

回答

1

你需要設置在你的配置功能的路由。該指令不是必需的,它不能像這樣工作。 看看這個: https://www.w3schools.com/angular/angular_routing.asp

+0

在html body中聲明'ng-app'和指令 – 2017-04-25 11:07:17

+0

在你的例子中,每個路由都有它自己的html模板?我想在身體內的幾個模板之間切換.... – n00n

+0

爲什麼我應該在體內聲明「ng-app」? – n00n