UI-Router Let URL Pass when not defined in a state

Matej

I would like to use Angular UI-Router however I would need it to let any url pass through the browser (and make the browser navigate the page) which isn't defined (the url) in the $stateProvider or $urlRouterProvider.

I've searched through their documentation, API etc but cannot find reference for this, other than $state.reload() which i've used before..

Jeff French

UPDATED: See update at the bottom.

From your comments I'm assuming the following:

  • Your Angular app lives at the root of the domain foo.com/
  • You are using HTML5 Mode so your app responds to "natural" urls like foo.com/thing1 and foo.com/thing2
  • You have some other app (not your main Angular app) that lives at foo.com/admin

Unfortunately, you can't have a url "pass through" like you want it to. :(

Your options are:

  • Move your Angular app into a subdirectory like foo.com/app. Then your app will respond to URLs like foo.com/app/thing1 and foo.com/app/thing2.
    • This is really the only choice if you want to use HTML5 routing mode to get "natural" looking urls.
  • Turn off HTML5 routing mode so that your Angular app lives at http://foo.com/#/ and responds to urls like foo.com/#/thing1 and foo.com/#/thing2. Then when your user visits foo.com/admin the url will fall through to the server and get whatever app is served there.

NOTE: This applies to both regular Angular routing using $routeProvider as well as using Angular-UI's $stateProvider.

UPDATE:

In order to get this to work you have to get the url change to happen outstide of Angular. Here's one method that will work but it has quite a smell to it IMO and will likely be quite fragile and difficult to maintain.

angular.module('myApp', [])
   .run(function($rootScope, $window){
      $rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl){
        // You'll probably want to just watch the path and build the full url
        // based on your environment, but this will get you started.
        if(newUrl === 'http://127.0.0.1/admin') {
          // prevent Angular from continuing with the location change
          event.preventDefault();
          // $window is an Angular wrapper around the global ``window`` object.
          // This causes the browser to go to the new url outside of Angular
          $window.location.href = newUrl;
        }
      });
});

Of course for this to work your server must be setup to serve some other app at the /admin path. Also, when your user navigates back to your Angular app it will lose all state as the Angular app will start up fresh again. Plus, your user will have to navigate back to the root of your Angular app. If the user goes from foo.com/ to foo.com/thing1 to foo.com/admin they will need to navigate back to foo.com/ to get back into your Angular app.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

angular.js ui-router pass variable to state url

From Dev

how to pass in state parameter to ui-router?

From Dev

UI-router abstract state with no url

From Dev

AngularJS / UI Router - Locale in State URL / TemplateURL

From Dev

How to pass url to $stateChangeStart of ui-router

From Dev

Angular UI-Router doesn't match a defined state

From Dev

Pass resolved objects into controllers defined by a string in angular-ui router

From Dev

Pass resolved objects into controllers defined by a string in angular-ui router

From Dev

reload state when you are on same state in angular ui-router

From Dev

UI Router: redirect to child state when navigating to abstract parent state

From Dev

UI-Router: state params in child state when parent abstract

From Dev

In AngularJS ui-router, state changed, url changed but template url not

From Dev

In AngularJS ui-router, state changed, url changed but template url not

From Java

How to pass an object into a state using UI-router?

From Dev

Angular UI Router - pass some params state in route, and some not?

From Dev

Angular-ui-router: how to pass parameters to $state.get?

From Dev

AngularJS UI-router pass param from view to state resolve

From Dev

Angular UI-router default state on same URL

From Dev

AngularJS ui-router otherwise to state instead of url?

From Dev

AngularJS UI-Router : Get the absolute URL of the state with the parameters

From Dev

Updating url on transition from child to parent state in UI router

From Dev

stateParams in url of abstract state, ui-router, angularjs

From Java

AngularJS UI Router - change url without reloading state

From Dev

Can't navigate to ui-router state with URL

From Dev

AngularJS ui-router default state for states without a URL

From Dev

Angular UI Router - Abstract Parent State with empty Url

From Dev

ui-router lazy load state using url

From Dev

Angular UI-Router parsing URL that contains slashes as part of state

From Dev

URL is not changing even after the state is changed . `ui-router`

Related Related

  1. 1

    angular.js ui-router pass variable to state url

  2. 2

    how to pass in state parameter to ui-router?

  3. 3

    UI-router abstract state with no url

  4. 4

    AngularJS / UI Router - Locale in State URL / TemplateURL

  5. 5

    How to pass url to $stateChangeStart of ui-router

  6. 6

    Angular UI-Router doesn't match a defined state

  7. 7

    Pass resolved objects into controllers defined by a string in angular-ui router

  8. 8

    Pass resolved objects into controllers defined by a string in angular-ui router

  9. 9

    reload state when you are on same state in angular ui-router

  10. 10

    UI Router: redirect to child state when navigating to abstract parent state

  11. 11

    UI-Router: state params in child state when parent abstract

  12. 12

    In AngularJS ui-router, state changed, url changed but template url not

  13. 13

    In AngularJS ui-router, state changed, url changed but template url not

  14. 14

    How to pass an object into a state using UI-router?

  15. 15

    Angular UI Router - pass some params state in route, and some not?

  16. 16

    Angular-ui-router: how to pass parameters to $state.get?

  17. 17

    AngularJS UI-router pass param from view to state resolve

  18. 18

    Angular UI-router default state on same URL

  19. 19

    AngularJS ui-router otherwise to state instead of url?

  20. 20

    AngularJS UI-Router : Get the absolute URL of the state with the parameters

  21. 21

    Updating url on transition from child to parent state in UI router

  22. 22

    stateParams in url of abstract state, ui-router, angularjs

  23. 23

    AngularJS UI Router - change url without reloading state

  24. 24

    Can't navigate to ui-router state with URL

  25. 25

    AngularJS ui-router default state for states without a URL

  26. 26

    Angular UI Router - Abstract Parent State with empty Url

  27. 27

    ui-router lazy load state using url

  28. 28

    Angular UI-Router parsing URL that contains slashes as part of state

  29. 29

    URL is not changing even after the state is changed . `ui-router`

HotTag

Archive