角度:将重定向转移到登录页面



我从Angular开始,完全迷失了。

首先,我调用一个简单的休息请求。问题是,目的地是外部登录页面的后面。因此,我的请求被重定向到外部页面,并且我的应用程序失败。

我想要的是打开我重定向到的页面。登录成功后,我应该将我重定向到第一个请求的页面(但这应该由外部页面完成,我猜)。

到目前为止,我的代码不是真的要先进,但这是:

var app = angular.module('webUI', [])
app.controller('Rest', function($scope, $http) {
    $http.get('https://test/rest/monitoring')
        .then(function successCallback(response) {
                $scope.rest = response.data;
            }, function errorCallback(response){
            });
});

我想念一个简单的角函数,那会做我要寻找的东西吗?

编辑:这是我在浏览器控制台(Chrome-> f12)中看到的错误消息:

从'https://test/test/rest/Monitoring'重定向到'https://some externalurl/internal/login?类型?在请求的资源上存在。因此,不允许访问原点" null"。

您必须将重定向的URL设置为$ location.path.path('/somenewpath');.。您必须在控制器中注入$ location的依赖。

      var app = angular.module('webUI', [])
      app.controller('Rest', function ($scope, $http, $location) {
          $http.get('https://test/rest/monitoring')
              .then(function successCallback(response) {
                  $scope.rest = response.data;
                  $location.path('/someNewPath');
              }, function errorCallback(response) {
              });
      });

您应该使用角路由,查看为什么在这里https://www.w3schools.com/angular/angular_routing.asp在这里,一个很好的示例https://ui-router.github.io/ng1/tutorial/helloworld,一旦配置路线,就可以执行此操作:

var app = angular.module('webUI', [])
app.controller('Rest', function($scope, $http) {
    $http.get('https://test/rest/monitoring')
        .then(function successCallback(response) {
                $scope.rest = response.data;
                $state.go('login');
            }, function errorCallback(response){
            });
});

最新更新