9

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

molerat
  • 946
  • 4
  • 15
  • 43

2 Answers2

5

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)

Philipp Andreychev
  • 1,638
  • 2
  • 14
  • 14
  • 1
    Thanks for you reply, but this does not work. But I have to say, I did not use the comment /* @ngInject */, assuming it's just a comment – molerat May 16 '15 at 14:49
  • @molerat I have to say, i did not set params in second argument of [$state.href](http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state) function. You should trying to set their. – Philipp Andreychev May 16 '15 at 14:55
  • I know, I set them their and it did not work. What url should `$state.href('stateName', {test: 'bla'}, {absolute: true}) ` return if we say the normal url would be http://localhost/stateName ? – molerat May 16 '15 at 14:58
  • @molerat don't fully get your question. You can configure your url just as you need to: [URL-Routing](https://github.com/angular-ui/ui-router/wiki/URL-Routing). Maybe you should trying to read this answer: [angularjs-pass-an-object-into-a-state-using-ui-router](http://stackoverflow.com/questions/20632255/angularjs-pass-an-object-into-a-state-using-ui-router) – Philipp Andreychev May 16 '15 at 16:32
  • @molerat in my case url should be equal `localhost/#/stateName/bla` :) – Philipp Andreychev May 16 '15 at 16:33
  • Okay Philipp, I'm sorry. I edited my question to make it more clear. Please take a look at it again – molerat May 16 '15 at 17:38
  • No, I ended up using GET-params in the url – molerat May 19 '16 at 14:42
0

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>
hamzeh.mm
  • 3
  • 5