2016-11-03 58 views
1

我正在使用AngularJS爲單聲道的前端構建.NET MVC。使用.NET MVC的角度路由

我有一個簡單的HomeController與一個簡單的return View()加載我的項目的主頁。

我的主要Index.cshtml的樣子:

<body ng-controller="AppController"> 
    <ul> 
    <li><a href="/#/routeOne">Route One</a></li> 
    <li><a href="/#/routeTwo">Route Two</a></li>    
    <li><a href="/#/routeThree">Route Three</a></li> 
    </ul> 
</body> 

我有我的JavaScript設置爲這樣:

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

    app.config(function ($routeProvider) { 
     $routeProvider.when('/routeOne', { 
      templateUrl: 'RoutesDemo/One' 
     }) 
     .when('/routeTwo', { 
      template: 'RoutesDemo/two' 
     }) 
     .when('/routeThree', { 
      template: 'RoutesDemo/three' 
     }); 
    }); 

     //var app = angular.module('app'); 
    app.controller('AppController', function ($scope) { 
     $scope.test = 'Hello World'; 
    }); 
})(); 

所以在這一點上我預期的行爲是,如果我點擊Route One鏈接時,應用程序應調用RoutesDemoController's One方法,該方法呈現一些局部視圖:「一個。有效!

但這不會發生。

取而代之,只有鏈接發生了變化,但在瀏覽器或網絡選項卡中沒有看到任何活動。

我錯過了一個步驟?

編輯:

我看打Index.cshtml主要就是頁面localhost:8080

我能打到localhost:8080/RoutesDemo/One它不會返回我的部分觀點。

當我在我的錨鏈接點擊路線一,我的網址是http://localhost:8080/#/routeOne

+0

先給像templateUrl的HTML模板: 'RoutesDemo/One.html'。你期待Angular能夠調用MVC路由嗎? –

+0

@SanishJoseph我試着將該模板重命名爲'.html'。它仍然沒有路線我 – Rhs

+0

你有一個像One.html這樣的路徑的實際HTML? –

回答

2

你不必爲你的路線模板的出口被寫入。 Angular爲此提供了一個特殊指令ng-view。當路線提供者處理特定路線時,ng-view元素的.innerHtml被動態替換爲templatetemplateUrl屬性的內容。 https://docs.angularjs.org/api/ngRoute/directive/ngView

更改索引是這樣的:

<body ng-controller="AppController"> 
    <ul> 
    <li><a href="/#/routeOne">Route One</a></li> 
    <li><a href="/#/routeTwo">Route Two</a></li>    
    <li><a href="/#/routeThree">Route Three</a></li> 
    </ul> 
    <ng-view /> 
</body> 
+0

我認爲這是已經存在於他所遵循的鏈接,

http://www.codeproject.com/Articles/806029/Getting-started -with-AngularJS-and-ASP-NET-MVC-Par –

+0

這個問題並沒有表明OP在跟着一個教程,他們可能已經在評論中說過了;我只是簡單地通過發佈在問題主體中的代碼,*顯然*不顯示此組件。 – Claies

+0

是的。確實如此。 –