2013-10-05 102 views
65

我正試圖讓AngularJS 1.2 RC2應用程序啓動並運行。目前,我一直在使用Angular Seed項目來嘗試啓動並運行我的應用程序。不幸的是,Angular Seed項目使用v1.0.7。從角種子工程,我已經更新了依賴關係如下所示:

$script([ 
    'res/js/angular-1.2.0-rc.2.js', 
    'res/js/angular-route-1.2.0-rc.2.js', 
    'res/js/app.js?v=2', 
], function() { 
    // when all is done, execute bootstrap angular application 
    angular.bootstrap(document, ['myApp']); 
}); 

在app.js,我有以下幾點:

'use strict'; 

angular.module('myApp', []). 
    config(['$routeProvider', function($routeProvider) { 
     $routeProvider.otherwise({redirectTo: '/home'}); 
    }]); 

當我運行這個程序,我得到的以下錯誤:

Error: [$injector:unpr] Unknown provider: $routeProvider 

我讀過一些這樣說:1的其他反應的)進樣「ngroute」或2)您需要定義在路由控制器。我的問題是,我不明白如何注入ngroute。另外,我真的需要在路線中定義控制器嗎?這種方法看起來不可擴展。我的應用可能有一千個視圖。在我看來,似乎必須有方法來定義路線,而不必加載所有的控制器。

+3

您是否嘗試在您的'myApp'模塊定義中添加ngRoute,例如'angular.module('myApp',['ngRoute'])。' – Chandermani

+0

這個問題應該在Angular js視頻教程中進行關聯,其中的一切工作在教練演示上都很完美。 – Rohit

回答

27

在角1.4 +,除了加入的依賴性

angular.module('myApp', ['ngRoute']) 

,我們還需要引用單獨的角route.js文件

<script src="angular.js"> 
<script src="angular-route.js"> 

看到https://docs.angularjs.org/api/ngRoute

141

它看起來像你忘了將ngRoute模塊包含在myApp的依賴項中。

在Angular 1.2中,他們使ngRoute成爲可選項(因此您可以使用第三方路由提供程序等),並且必須明確依賴模塊以及including the separate file

'use strict'; 

angular.module('myApp', ['ngRoute']). 
    config(['$routeProvider', function($routeProvider) { 
$routeProvider.otherwise({redirectTo: '/home'}); 
}]); 
+1

保存我的屁股。升級到Angular 1.2謝謝 –

+1

這個答案在嘗試從http://yeoman.io/codelab.html運行todo應用程序的'dist'版本時拯救了我的生命 - 所以謝謝Cuong Vo! – kasperwf

+1

這意味着當你測試它時,你只需要提供它作爲模塊的參數,而不是在加載模塊進行測試時作爲參數。 – Katana24

相關問題