在mvc控制器中登录后出现angularJs重定向问题

时间:2022-10-20 10:32:26

I'm facing a issue. Im developing a login form with angular + mvc 4.The problem starts when I try to redirect to another view after the login process.

我正面临一个问题。我正在开发一个带有角度+ mvc 4的登录表单。当我尝试在登录过程后重定向到另一个视图时,问题就出现了。

Code:

This is my factory: This factory execs an ajax call to my action result in a mvc controller, and it works well.

这是我的工厂:这个工厂在mvc控制器中对我的动作结果执行ajax调用,效果很好。

moduloRegistro.factory('loginRepository', function ($resource) {
    return {
        login: function (usuario) {
            return $resource('Acesso/Logar').save(usuario);
        }
    }
});

Below my controller: My controller validate the result and if everything is ok it tries to redirect to the new URL

在我的控制器下面:我的控制器验证结果,如果一切正常,它会尝试重定向到新的URL

moduloRegistro.controller('LoginController', function ($scope, loginRepository, $location, toastr) {
    $scope.erroLogin = '';
    $scope.login = function (usuario) {
        loginRepository.login(usuario).$promise.then(
            function (response) {
                if (response.mensagemDeErro != undefined || response.mensagemDeErro != null)
                    toastr.error(response.mensagemDeErro);
                else
                    $location.url('Board/Programa');
            },
            function (erro) {
                toastr.error(erro.mensagemDeErro);
            }
        );
    };
});

And this is my module: Where I configure the routes and templates to be rendered into the ng-view.

这是我的模块:我在哪里配置要渲染到ng-view中的路由和模板。

var moduloRegistro = angular.module('moduloRegistro', ['ngRoute', 'ngResource']);


moduloRegistro.config(function ($routeProvider, $locationProvider) {
    $routeProvider.when('/',
        {
            templateUrl: '/Templates/Acesso/login.html', controller: 'LoginController'
        });
    $routeProvider.when('/Board/Programa',
        {
            templateUrl: '/Templates/Board/programa.html', controller: 'BoardController'
        });
    $locationProvider.html5Mode(true)
});

moduloRegistro.value('toastr', toastr);

This is my RouteConfig:

这是我的RouteConfig:

routes.MapRoute(
              name: "Board SPA",
              url: "Board/{*catchall}",
              defaults: new { controller = "Board", action = "Index" }
          );

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Acesso", action = "Index", id = UrlParameter.Optional }
            );

The problem: When he redirect to the new view, the BoardController didnt work and the html from my main view spa page are rendered inside the ng-view.

问题:当他重定向到新视图时,BoardController没有工作,我的主视图spa页面中的html在ng-view中呈现。

Bellow the controller and factory of my Board:

Bellow我的董事会的控制人和工厂:

//Controller.
moduloRegistro.controller('BoardController', function ($scope, boardRepository, $location, toastr) {
    $scope.partes = boardRepository.getPartes();
});

//Factory
moduloRegistro.factory('boardRepository', function ($resource) {
    return {
        getPartes: function () {
            return $resource('Board/ObterPartesEscola').get();
        }
    }
});

2 个解决方案

#1


0  

Take a look at the resolve function that you can use when you define your routes. More info can be found within this answer

查看定义路径时可以使用的resolve函数。在这个答案中可以找到更多信息

#2


0  

Not the solution to this specific problem but just a bit of code-style...

不是这个特定问题的解决方案,而只是一些代码风格...

This:

moduloRegistro.config(function ($routeProvider, $locationProvider) {
$routeProvider.when('/',
    {
        templateUrl: '/Templates/Acesso/login.html', controller: 'LoginController'
    });
$routeProvider.when('/Board/Programa',
    {
        templateUrl: '/Templates/Board/programa.html', controller: 'BoardController'
    });
$locationProvider.html5Mode(true)
});

Looks much better like this:

看起来好多了:

 moduloRegistro.config(function ($routeProvider, $locationProvider) {
    $routeProvider.when('/',
        {
            templateUrl: '/Templates/Acesso/login.html', 
            controller: 'LoginController'
        })
    .when('/Board/Programa',
        {
            templateUrl: '/Templates/Board/programa.html', 
            controller: 'BoardController'
        });
    $locationProvider.html5Mode(true)
 });

Whenever you use a service more than once you can chain them together by not using a semicolon and following with '.whatever' .

每当您多次使用某项服务时,您可以通过不使用分号并使用“.whatever”进行链接将它们链接在一起。

#1


0  

Take a look at the resolve function that you can use when you define your routes. More info can be found within this answer

查看定义路径时可以使用的resolve函数。在这个答案中可以找到更多信息

#2


0  

Not the solution to this specific problem but just a bit of code-style...

不是这个特定问题的解决方案,而只是一些代码风格...

This:

moduloRegistro.config(function ($routeProvider, $locationProvider) {
$routeProvider.when('/',
    {
        templateUrl: '/Templates/Acesso/login.html', controller: 'LoginController'
    });
$routeProvider.when('/Board/Programa',
    {
        templateUrl: '/Templates/Board/programa.html', controller: 'BoardController'
    });
$locationProvider.html5Mode(true)
});

Looks much better like this:

看起来好多了:

 moduloRegistro.config(function ($routeProvider, $locationProvider) {
    $routeProvider.when('/',
        {
            templateUrl: '/Templates/Acesso/login.html', 
            controller: 'LoginController'
        })
    .when('/Board/Programa',
        {
            templateUrl: '/Templates/Board/programa.html', 
            controller: 'BoardController'
        });
    $locationProvider.html5Mode(true)
 });

Whenever you use a service more than once you can chain them together by not using a semicolon and following with '.whatever' .

每当您多次使用某项服务时,您可以通过不使用分号并使用“.whatever”进行链接将它们链接在一起。