【问题标题】:How to redirect using ajax in angularjs如何在angularjs中使用ajax重定向
【发布时间】:2016-06-26 15:13:57
【问题描述】:

我想在 angularjs 中使用 ajax 调用重定向特定页面上的用户。我可以使用下面提到的代码进行重定向,但是当我再次想将用户重定向到根页面时,我无法这样做,因为 $window.location.href+ 'getTechnicianWorkOrder/'+woId 的值保持不变:

  $scope.getTechnicianWorkOrderFormURL = function(woId){ 
       return $window.location.href + 'getTechnicianWorkOrder/'+woId;   
     };

另一个我想重定向到根页面的地方:

 $scope.getAssignedListURL = function(){     
    return $window.location.href;
};

注意:即使在 HTML5 缓存清单的离线模式下,我也想让这个重定向工作。

【问题讨论】:

    标签: angularjs ajax html html5-appcache


    【解决方案1】:

    使用$location 服务。

    $location.path('/');

    【讨论】:

    • 在哪里使用它?你可以编辑我上面的代码示例并显示在哪里使用它吗?
    • 我猜你有一个 ng-click 你的应用程序或一些调用函数返回根页面的 ajax 回调,在该函数中放置代码。
    【解决方案2】:

    @prashant-palikhe 的答案是正确的,$location.path('/'); 是通往根路径的路径。只需像这样使用$location 对您的controller 的依赖即可:

    yourapp.controller('YourController', ['$location', function($location) {
        ...
    } 
    

    我总是将ui.router 用于我的路线,您可以在其中添加$urlRouterProvider.otherwise('/') 以作为任何未知stateroute 的后备。

    在某些情况下,您可以在您的州添加类似的内容:

    resolve : {
        dataObj : ['$http', function($http) {
            return $http({method : 'GET', url : '/your/ajax/endpoint'})
        ;}],
    },
    onEnter : ['dataObj', '$state', function(dataObj, $state) {
        // dataObj is your ajax response object. Based on this you can redirect to a certain state of needed
        $state.go('default');
    }]
    

    resolve是预加载数据的数据onEnter在进入状态之前被调用。这可以用作某种中间件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 2016-11-21
      • 2012-08-08
      • 1970-01-01
      • 2014-09-23
      相关资源
      最近更新 更多