如何使用AngularJS重定向ASP MVC中的页面? [英] How to redirect pages in ASP MVC using AngularJS?

查看:81
本文介绍了如何使用AngularJS重定向ASP MVC中的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历了启动新的ASP.NET MVC无用户身份验证过程的过程,并且已经开始将AngularJS与C#代码集成.

I went through the process of starting a new ASP.NET MVC no-user authentication process, and have already began integrating AngularJS with C# code.

对于在视图"->主页"下找到的每个视图",我的_ViewStart.cshtml使用_Layout.cshtml@RenderBody().我有两个视图,分别是Index.cshtmlSignUp.cshtml.

My _ViewStart.cshtml uses _Layout.cshtml to @RenderBody() for each of my Views found under Views->Home. I have my two views, Index.cshtml and SignUp.cshtml.

在Index部分视图中,我有一个表单,该表单具有受此AngularJS函数控制的动作:

In the Index partial view, I have a form that has an action controlled by this AngularJS function:

.controller('LoginController', function ($scope, $location, LoginService) {
    $scope.LoginData = {
        CID: '',
        Email: ''
    };

    $scope.Login = function ($window) {
        $scope.Submitted = true;
        if ($scope.IsFormValid) {
            LoginService.GetUser($scope.LoginData).then(function (d) {
                if (d.data.CID != null) {
                    loginCID = d.data.CID;
                    loginEmail = d.data.Email;
                    $scope.Message = "Successfully logged in. Welcome " + d.data.CID;
                    $location.path("/Home/SignUp");
                }
                else {
                    alert("Invalid Creds");
                }
            });
        }
    };
})

我已省略了此问题并非完全必要的任何额外代码,因此已排除了额外的变量/工厂/服务.该代码除以下行外均适用:

I've omitted in included any extra code that wasn't entirely necessary for this question so extra variables/factories/services have been excluded. The code all works except for this line:

$location.path("/Home/SignUp");

一旦填写了表单,$scope.Message就会更改为if语句中的内容,URL将从http://localhost:61660/更改为http://localhost:61660/Home/SignUp,但仍会显示Index的部分视图.意思是,我仍然看到Index.cshtml在主体中呈现,而不是SignUp.cshtml.如何更改它以实际重定向?

Once the form is filled out, $scope.Message changes to what's in the if statement, and the URL changed from http://localhost:61660/ to http://localhost:61660/Home/SignUp But the partial view for Index still shows. Meaning, I'm still seeing Index.cshtml being rendered in the body and not SignUp.cshtml. How can I change this to actually redirect?

推荐答案

尝试使用此方法代替$location.path("/Home/SignUp"):

window.location.pathname = 'Home/SignUp';

这是因为角度拦截了$location的重定向并通过自己的路由基础结构进行处理,从而防止您的预期行为.

It is caused, because angular intercepts $location's redirections and process them by means of own's routing infrastructure, preventing your's expected behavior.

这篇关于如何使用AngularJS重定向ASP MVC中的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆