Angular uiouter在新窗口或选项卡中使用statparams打开状态

Angular uiRouter open state in new window or tab with stateParams

本文关键字:statparams 状态 选项 新窗口 uiouter 窗口 Angular      更新时间:2023-09-26

我想做的是在新的选项卡或新窗口中跟随:

$state.go('studentsReport', {
       type: $scope.report.type, // string
       selectedStudents: $scope.selectedStudents // array of strings
});

如果我做了:

var link = $state.href('studentsReport', {
       type: $scope.report.type,
       selectedStudents: $scope.selectedStudents
});
window.open(link, '_blank');`

我将失去参数。

最诚挚的问候,马塞尔。

你应该尝试这样做:

/* @ngInject */
function SomeCtrl ($state, $window) {
  $window.open($state.href('stateName', {}, {absolute: true}), '_blank');
}

注意:如果使用ng-annotate(可在cli, gulp和grunt版本中使用),/* ngInject */可以实现自动依赖注入注释

使用ng-click链接标签并调用函数。在函数中把你的参数放在LocalStorage中。然后在app.run中使用$rootScope.$on("$stateChangeStart")并检查localstorage是否有参数获取params并调用$state。

//in page controller: 
var openNewTab = function () {                  
                    localStorage.newTab = JSON.stringify({
                        state: "yourState",
                        params: {
                            param1: "someparam1",
                          param2:"someparam2"
                        }
                    });                  
                    window.open(document.location.origin);
                      
                }
 
 //angular app run config: 
angularApp.run(function($state){
if(localStorage.newTab){
   var newTab = JSON.parse(localStorage.newTab);
   localStorage.removeItem("newTab");
   $state.go(newTab.state, newTab.params);
   event.preventDefault();
}
})
<a ng-click="openNewTab()" >open new tab</a>