2015-04-05 104 views
3

主頁關於頁面有簡單的標題內容,它在運行頁面時沒有顯示。相反,它顯示:簡單的AngularJS路由不起作用

Error: $injector:modulerr
Module Error".

我該如何解決這個問題?

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

 
app.config(function ($routeProvider) { 
 
    $routeProvider. 
 
     .when('/', { 
 
     \t templateUrl : 'partial/home.html', 
 
     }) 
 
     .when('/about', { 
 
     \t templateUrl: 'partial/about.html', 
 
     }) 
 
     otherwise({ redirectTo: '/'}) 
 
});
<div class="container"> 
 
    <div> 
 
     <nav> 
 
      <a href="#/">Home</a> 
 
      <a href="#/about">About</a> 
 
     </nav> 
 
    </div> 
 
</div>

回答

0

此外,你應該使用$urlRouterProvider

$urlRouterProvider.otherwise('/'); 
+0

這也是行不通的!:(我是AngularJS的新手,所以不知道代碼究竟是什麼問題 – Learner 2015-04-06 18:02:36

+0

如果你的應用程序只包含您在第一篇文章中展示的代碼,問題可能是您沒有安裝ng-route的節點包。您是否使用yeoman生成項目?爲了幫助您,我需要您更新plnkr。我建議你使用[ui-router](https://github.com/angular-ui/ui-router)而不是ngRoute [這裏是一個簡單的使用ui-router的例子](http:// plnkr。 co/edit/JVB5Jg?p = info) – Matho 2015-04-07 03:40:52

+0

感謝Matho !!我在本地瀏覽器上運行我的代碼,當我嘗試使用本地主機時,它工作正常:) – Learner 2015-04-14 19:28:31

2

雖然我還沒有遇到你的代碼,我可以看到一個語法錯誤。

編輯:其實你有兩個語法錯誤,而不是錯過了'。',你已經在$ routeProvider之後添加了兩個。

app.config(function ($routeProvider) { 
$routeProvider. //Two '.'s 
    .when('/', { 
     templateUrl : 'partial/home.html', 

    }) 
    .when('/about', { 
     templateUrl: 'partial/about.html', 

    }) 
    //No '.' here 
    otherwise({ redirectTo: '/'}) 
}); 

所以用'。'替換上面的,所以函數鏈不會中斷。

app.config(function ($routeProvider) { 
$routeProvider 
    .when('/', { 
     templateUrl : 'partial/home.html', 

    }) 
    .when('/about', { 
     templateUrl: 'partial/about.html', 

    }) 

    .otherwise({ redirectTo: '/'}) 
}); 
+0

感謝李!語法不見了,但仍然有錯誤:$ injector:modulerr 模塊錯誤「。 – Learner 2015-04-06 18:01:54