2016-02-01 41 views
1

我想重定向到應用程序的另一個頁面。與我們在MVC應用程序或asp.net應用程序中採用的方式類似。我已經定義了下面的Route.js文件。以下列方式定義我想重定向到另一個頁面使用角度JS

route.js

var MainApp=angular.module('RoutingApp', ['ngRoute']) 
    .config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) { 
     $routeProvider 
      .when('/BusinessAgent', { 
       templateUrl: 'Views/BusinessAgent/BusinessAgent.html', 
       controller: 'BusinessAgentController' 
      }) 
     .when('/Admin', { 
      templateUrl: 'Views/Admin/Admin.html' 
     }) 
      .otherwise({ 
       redirectTo: '/' 
      }); 
     $locationProvider.html5Mode({ 
      enabled: true, 
      requireBase: false 
     }); 
    }]); 

HTML頁面

<section id="banner">   
     <ul class="actions"> 
        <li> 
         <a href="#BusinessAgent" class="button big">Business Agent</a> 
        </li> 
       </ul> 
    </section> 

在HREF的點擊就應該redirect.But它不被重定向。

+0

在控制檯中的任何錯誤? –

回答

-2

可以使用

$window.location.href = '/index.html'; 

爲redurect到另一頁

+0

OP明確要求幫助ngRoute ... –

0

我認爲你缺少NG-視圖。爲路由添加ng-view。它可以作爲佔位符

<div ng-view></div> 
+0

它應該是部分視圖。它應該重定向到新頁面 –

0

如果您正在使用html5Mode啓用

$locationProvider.html5Mode({ 
    enabled: true, 
    requireBase: false 
}); 

然後替換 '/' '#' 在HREF

<a href="/BusinessAgent" class="button big">Business Agent</a> 

如果你禁用html5Mode然後使用

<a href="#/BusinessAgent" class="button big">Business Agent</a> 
0

試着把控制器的網址,而不是在那裏查看。 而不是

$routeProvider 
     .when('/BusinessAgent', { 
      templateUrl: 'Views/BusinessAgent/BusinessAgent.html', 
      controller: 'BusinessAgentController' 
     }) 

試着用這個。

  $routeProvider 
     .when('/BusinessAgent', { 
      templateUrl: 'ControllerName/Actionname', 
      controller: 'BusinessAgentController' 
    }) 

如果您試圖獲得不同的視圖,則使用局部視圖來控制器的相應操作。

0

對我來說,我喜歡使用$ urlRouterProvider而不是$ routeProvider看下面的例子:

$urlRouterProvider.otherwise("/BusinessAgent"); 

     $stateProvider 
     .state("BusinessAgent", { 
      url: "/BusinessAgent", 
      templateUrl: "Views/BusinessAgent/BusinessAgent.html", 
      controller: "BusinessAgentController" 
     }); 

這意味着你將調用狀態SREF而不是HREF這樣的:

<a sref="BusinessAgent" >BusinessAgent</a> 
0

我看到你使用了Angular UI-Router。

這是我最近使用的應用程序。使用ui-sref標記而不是href標記。

<a ui-sref="/">Back to home</a> 

<a ui-sref="cafe">Back to Cafe State</a> 

我已經使用狀態提供程序,而不是路由提供程序。我的狀態是

根州

$stateProvider.state('/',{ 
    url:"/", 
    templateUrl:'js/apps/choiceScreen/choice.html', 
    controller:'choiceCtrl' 
}) 

咖啡館國家

.state('cafe',{ 

templateUrl:'js/apps/cafe/cafeScreen.html', 
     controller:'cafeCtrl' 
}); 
+0

你可以舉例說明,因爲從來沒有使用$ stateProvider –

+0

是的,勾選這個[link](https://github.com/angular-ui/ui-router/wiki) –

相關問題