1

I have a component (directive) which should be used as a stand alone component by other applications (bower component, for example). I want to add navigation inside my directive (using ngRoute or ui-router) - but this could conflict with the host application navigation (if it uses one).

For example:

  • Main application has a routing for my component:

http://address.com/route-to-my-component

Now, inside my application I should have an inner route (I am not aware of the main application routing):

$routeProvider.
      when('/route1', {
        template: `<comp1></comp1>`
      }).
      when('/route2', {
        template: `<comp2></comp2>`
      }).
      otherwise({
        redirectTo: '/route1'
      });

There are several problems with this solution:

  1. Main application might not recognise my routing, so it will fall to its default route.
  2. I cannot know what is my parent router solution, which makes it hard for me to configure my own routes.

Is there a solution to this problem?

Thanks

EDIT

I ended up with two optional solutions:

  1. Using angular-widget, which is a great solution but is too complex for my case (it will be an overkill for me)

  2. Using history.pushState.

So, I implemented the second option, like this:

  • When the use click the navigation (inside my component), I do pushState:

    history.pushState({someData}, 'some-title', url );

  • And I added an event for user's pressing the back button:

    window.onpopstate = function(event) { //My back logic here }.bind(this);

It is kind of hackey solution but it was perfect to my situation and needs.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Yaniv Efraim
  • 6,633
  • 7
  • 53
  • 96
  • Since you are using a directive instead of manage navigation thought the router you could create a directive that adds an remove the other directives from the template with compile. This might be cumbersome but is the first idea it come to my mind since you don't wan't to mess up with the container application routing. – Raulucco Jan 06 '16 at 10:28
  • ok - but this way I will not have navigation (navigating back/forward using browser navigation, inside my component), right? I want the user to be able to navigate back/forward inside my inner directive, since I have several screens. – Yaniv Efraim Jan 06 '16 at 10:34
  • Yeah, this could be an issue. You might want to create a service to handle the navigation. What about a provider that the user can set a base url to use on your configuration? That way if someone wants to use your component can pass a base url. – Raulucco Jan 06 '16 at 10:41

0 Answers0