molerat
molerat

Reputation: 994

Angular uiRouter open state in new window or tab with stateParams

What I want to do is following in but in a new Tab or new Window:

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

If I did:

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

window.open(link, '_blank');`

I would lose the parameters.

Best regards, Marcel

Upvotes: 9

Views: 10561

Answers (2)

hamzeh.mm
hamzeh.mm

Reputation: 3

use ng-click on link tag and call a function. in function put your parameters in LocalStorage. then in app.run use $rootScope.$on("$stateChangeStart") and check if localstorage have parameters get params and call $state with params.

//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>

Upvotes: 0

Philipp Andreychev
Philipp Andreychev

Reputation: 1648

You should trying to use this:

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

Note: the /* ngInject */ facilitates automatic dependency injection annotation if using ng-annotate (available in cli, gulp, and grunt flavours)

Upvotes: 5

Related Questions