我想使用angular JS重定向到另一个页面

时间:2016-02-01 10:44:46

标签: javascript jquery angularjs angular-ui-router

我想重定向到应用程序的另一个页面。以与我们在MVC应用程序或asp.net应用程序中类似的方式。我已经定义了下面的Route.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后,它应该重定向。但它没有被重定向。

6 个答案:

答案 0 :(得分:0)

我认为你错过了ng-view。添加ng-view进行路由。它作为占位符

<div ng-view></div>

答案 1 :(得分:0)

如果您使用的是html5Mode

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

然后替换#&#39;#&#39;用&#39; /&#39;在href

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

如果您禁用了html5Mode,请使用

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

答案 2 :(得分:0)

尝试放置控制器的url而不是在那里查看。 而不是

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

尝试使用它。

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

如果您尝试获取不同的视图,请使用部分视图来执行控制器的相应操作。

答案 3 :(得分: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>

答案 4 :(得分: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'
})

Cafe State

.state('cafe',{

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

答案 5 :(得分:-2)

你可以使用

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

用于redurect到另一页