Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular routing without changing location

Chrome Packaged Apps have a rather strict Content Security Policy. One result of this is that manipulating the location (like clicking on a link) results in:

'Can't open same-window link to "chrome-extension://lkjasdfjklbdskjasdfjkhfdshjksad/derp.html"; try target="_blank". '

Target _blank will open the link in chrome which is not what I want. Can AngularJS' routing work in such a locked-down environment?

They docs give an example of an Angular app, but conspicuously does not use routing.

Update

Here is the link that, when clicked, gives the error: <a class='walrus-link' ng-href='paystubs/{{walrus.id}}'>Walrus {{id}}!</a>

like image 802
Chris Avatar asked Mar 13 '13 17:03

Chris


People also ask

Can we have routing without URL?

You can perform routing without changing the URL with the help of NavigationManager. You need to inject NavigationManager to use this service. You can use this service to perform routing programmatically.

What is the purpose of wildcard route in Angular?

Setting up wildcard routeslink To add this functionality to your application, you set up a wildcard route. The Angular router selects this route any time the requested URL doesn't match any router paths. To set up a wildcard route, add the following code to your routes definition.

Will Angular support only path location strategy?

The big benefit of Angular Universal is that pages can be cached on the server side and applications will then load much faster. For Angular Universal to work URL s need to be passed to the server side which is why it can only work with a PathLocationStrategy and not a HashLocationStrategy .

Can we use 2 router-outlet in Angular?

You can have multiple router-outlet in same template by configuring your router and providing name to your router-outlet, you can achieve this as follows. Advantage of below approach is thats you can avoid dirty looking URL with it. eg: /home(aux:login) etc. Assuming on load you are bootstraping appComponent.


1 Answers

Instead of using an href, try using ng-click and call a method to your controller the relocates to the appropriate page using $location. See the documentation on the AngularJS site. The following quote from the doc gives an indication that the $location service might be appropriate for you:

When should I use $location? Any time your application needs to react to a change in the current URL or if you want to change the current URL in the browser.

Your code might look something like this:

<a class='walrus-link' ng-click='getPaystub(walrus.id)'>Walrus {{id}}!</a>

and in your parent controller, you'll need a scope method called 'getPaystub' with a line similar to:

$scope.getPaystub = function(selectedWalrusId) {
  $location.path('paystubs/'+$scope.walrus.id);
}

This way angular keeps control and won't cause a page refresh. This hopefully keeps you within the bounds of the CSP. Unfortunately I cannot test this in a packaged app, but I've used the exact same convention in a web app and it works just dandy.

like image 133
eterps Avatar answered Sep 27 '22 21:09

eterps