Angular routerLink directive in modal component not working as expected

valentin.mu

I encountered pretty strange situation with routerLink directive used in NgbModal. To start, I have feature module with structure like this:

-feature/
  -components/
    -popup-component/
      popup-component.ts
    -preferences-component/
      preferences-component.ts
    index.ts
  +containers/
  feature-routing.module.ts
  feature.module.ts

The routing is defined like this(simpified):

export const routes: Routes = [
  {
    path: '',
    component: ContainerPageComponent
  },
  {
    path: ':id',
    component: AnotherContainerPageComponent,
    children: [
      { path: '', redirectTo: 'some-route' },
      { path: 'preferences', component: PreferencesComponent },
      { path: 'rules/new', component: OtherComponent }
    ]
  }
];

I have modal that is opened from preferences component like this:

  addRulePopup() {
    const ref = this.modalService.open(PopupComponent, {
      backdrop: 'static',
      centered: true,
      size: 'lg',
      backdropClass: 'bg-white-opaque force-opaque no-border',
      windowClass: 'no-border'
    });
    ref.result.then(e => {
      console.log(e);
    });
  }

I don't change url when opening modal so i suggest that from router's perspective nothing changes when i open modal.

In both popup-component and preferences-component i have link like this:

<div class="rule" routerLink="../rules/new">

The problem is that from popup-component i'm redirected to 404, while from preferences i navigate to right url.

Description of problem may seem overcomplicated, so sorry if you feel so :) i tried to simplify things.

valentin.mu

i have already resolved the problem by using router.navigate from controller, passing activated route instance, like this:

this.router.navigate(['../../preferences'], { relativeTo: this.route });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular 2 component directive not working

Angular 1 directive component has different behavior in modal and out of the modal

Angular directive not working in dynamically loaded component

routerLink not working in Angular 6

RouterLink is not working in Angular 6

routerLink is not working in angular 2

In Angular 2+, can I detect when a directive (like routerLink) is applied to my custom component?

Angular's ng include directive not working for the expected link

$modal is not working as expected

Why angular router reload page with routerLink directive?

Angular routerlink not working with lazyloading modules

Bootstrap Modal popup component not working on click of angular formly element

Modal popup is not working in hierarchical component structure angular-bootstrap-md

Passing modal form data to another component not working in Angular

Spray authorized directive not working as expected?

@Directive vs @Component in Angular

Angular component (with a directive) testing

Collapse component not working as expected

Angular service not working as expected when calling a function in component

Ionic 3: Custom angular component not working as expected. Bug?

Angular-ui modal directive

Modal confirmation as an Angular UI directive

Angular Directive to component angular 1.5

How to use Storybook with a component using [routerLink] (Angular)?

Angular 6 navbar routerlink reload component

A simple angular directive not working

Angular directive with scope : @ not working

ngSmoothScroll angular directive not working

Angular Directive templateUrl Not Working