2013-05-09 118 views
0

我是新來的angularjs,只是試圖建立一個與它的CRUD應用程序。AngularJS路由配置

我的代碼是這樣的http://jsfiddle.net/fpnna/1/

,我使用Apache作爲Web服務器,在

$locationProvider.html5mode(true); 

圖靈然後讓

uncaught TypeError: Object #<Ic> has no method 'html5mode' from testApp 

當我點擊「添加新時「路徑變成」/新「但出錯

404 The requested URL /new was not found on this server. 

任何想法,我做錯了。

我經歷了官方手冊,想不起來。

在此先感謝。

+2

我討厭它,當有人得到downvotes案件錯字。我們都已經做到了,而且我們中的很多人都前進並把它扔在這裏,並感到羞愧。但是,OP將錯誤和問題放在那裏的事實意味着其他人正在進行20小時以上的衝刺,無法再打字或思考,因此可以輕鬆地將其從列表中刪除。謝謝OP。當它最重要的時候,你可能救了我20分鐘。 – 2014-03-03 05:25:35

回答

5

你有幾個問題,首先在jsfiddle你不需要身體標籤加上你有多個身體標籤。你的小提琴還有兩個ng-apps,路線定義不正確(例如應該是/ new),無效的ng-view結束標記,應該只有一個。您應該在模板中包含javascript並且不打包,最後是html5Mode,模式中的首字母爲M,您的部分內容不存在於其url中,也不定義爲本地腳本。

我會建議你使用plunkr,因爲它允許你添加其他本地文件,即你的部分不存在於小提琴中。

我已經清理了所有關於這個plunkr的問題:http://plnkr.co/edit/A23Fxn9Ji02TGZ0jouZR?p=preview

angular.module('testApp', []). 
config(function ($routeProvider, $locationProvider) { 
    $locationProvider.html5Mode(true); // case is important 
    $routeProvider. 
    when("/", { 
     templateUrl: "list.html" 
    }). 
    when("/new", { // make sure and use absolute link to route 
     templateUrl: "edit.html" 
    }) 
}) 

function testCtrl($scope) { 
    $scope.persons = [{ 
     name: "X" 
    }, { 
     name: "Y" 
    }, { 
     name: "Z" 
    }] 
} 

和HTML:

<body ng-controller="testCtrl" > 
    <div class="main-nav"> <a href="new">Add Me</a> 
    </div>INDEX 
    <div > 
     <div ng-view> 

     </div> 
    </div> 
</body> 

請仔細閱讀文檔和教程來學習關於建立基礎項目。 http://docs.angularjs.org/guide/bootstrap

+0

路由問題呢?你能解釋一下嗎? – arnold 2013-05-09 13:34:07

+0

@arnold,我的答案中的plunkr顯示了正確的設置並與路由一起工作。單擊添加鏈接。此外,這裏是關於html5Mode的好鏈接。 http://stackoverflow.com/questions/13832529/how-to-config-routeprovider-and-locationprovider-in-angularjs你的小提琴永遠不會工作它有太多的問題與基本設置的方式。 – lucuma 2013-05-09 13:34:57

+0

非常感謝:) – arnold 2013-05-09 13:44:37