Angular Open state uiRouter in a new window or tab with stateParams

I want to do the following, 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 options.

Regards, Marcel

+6
source share
2 answers

You should try using this:

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

Note. /* ngInject */ facilitates automatic annotation of dependency injection if ng-annotate is used (available in cli , gulp and grunt flavors)

+5
source

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

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

Source: https://habr.com/ru/post/987337/


All Articles