简体   繁体   English

如何在Angular 1.5中重定向?

[英]How to redirect in Angular 1.5?

Complete beginner to AngularJs, how do I redirect to a certain page in this service method? 对AngularJs来说是完整的初学者,如何使用这种服务方法重定向到某个页面?

The destination should be "/#/login", I've tried with $location and $window but I might very well be missing something. 目的地应该是“ /#/ login”,我尝试使用$ location和$ window,但是我很可能会丢失一些东西。

Thanks 谢谢

 angular.module('app.services', ['ngRoute'])
    .service('TaskPlannerApiService', [
        '$http', '$q', '$timeout', function taskPlannerApiService($http, $q, $timeout) {
            var me = this;

            me.getDataAsync = function () {

                var logincheck = true;
                var apiCall = $q.defer();
                var timeoutHanlder = $timeout(function () {
                    apiCall.reject('Timed out');
                    console.log('timed out');
                }, 2000);

                $http.get('/api/login/check').success(function (chk) {
                    console.log(chk);
                    logincheck = chk;
                    console.log(logincheck);

                    if (chk === false) {
                        logincheck = false;
                    }

                    if (logincheck == true) {
                        console.log("/api/taskplanner");
                        $http.get('/api/taskplanner/').success(function (data) {
                            $timeout.cancel(timeoutHanlder);
                            apiCall.resolve(data);
                            console.log(data);
                        }).error(function (data) {
                            $timeout.cancel(timeoutHanlder);
                            apiCall.reject(data);
                            console.log(data);
                        });
                    }
                    else {
                        console.log("redirect");
                    }
                })

                $timeout(function () { apiCall.notify('Retrieving Data . . .'); }, 0);
                return apiCall.promise;
            };
        }
    ])

$location Inject this your controller $location注入您的控制器

Next, if you want the place to use the below code. 接下来,如果您希望该场所使用以下代码。

$location.path('/login');

If you are using angularjs internal routes than you have to inject $location in your controller and give this code below. 如果您正在使用angularjs内部路由,则必须在控制器中注入$ location并在下面提供此代码。

  $location.path( "/login" );

If you are using ui-router which is quite famous in angularjs then you will be dealing with states. 如果您使用的是在angularjs中非常有名的ui-router,那么您将要处理状态。 so in such case you have to inject $state in controller and change your state using this: 因此,在这种情况下,您必须在控制器中注入$ state并使用以下命令更改状态:

$state.go('state name')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM