can deactivate guard does not work when used on one component that is inside another











up vote
0
down vote

favorite












I'm having trouble making Can Deactivate work in my application. In the file form.component.html I have the following code:



   <div class="form-group">
<label for="quoteType">Quote Type</label>
<select class="form-control" id="quoteType" name="QuoteType" #type (change)="getQuoteType(type.value)">
<option selected="selected">---</option>
<option value="New">New</option>
<option value="Amendment">Amendment</option>
<option value="Migration">Migration</option>
<option value="Replacement/Renewal">Replacement/Renewal</option>
</select>
</div>

<div [ngSwitch]="quotetype">
<div *ngSwitchCase="'New'">
<app-form-new [quotetype]="quotetype"></app-form-new>
</div>
<div *ngSwitchCase="'Amendment'">
<app-form-amendment [quotetype]="quotetype"></app-form-amendment>
</div>
<div *ngSwitchCase="'Migration'">
<app-form-migration [quotetype]="quotetype"></app-form-migration>
</div>
<div *ngSwitchCase="'Replacement/Renewal'">
<app-form-replacement-renewal [quotetype]="quotetype"></app-form-replacement-renewal>
</div>
</div>


Depending on what I select will render a different component in this template. In this case I will select "New"!



Image before selecting:
Image 1



After New is selected:
Image 2



What I want is: if the user starts filling out the form and unwittingly or purposely hit the back button to the previous page, a message appears asking if he really wants to do this.



I can do it on other pages of this system, but when I go to those forms it does not work. Below is the code I have made so far.



new-can-deactivate-guard.service.ts

import { Injectable } from '@angular/core'
import { CanDeactivate } from "@angular/router";
import { FormNewComponent } from './form-new.component'

@Injectable({
providedIn: 'root'
})
export class FormNewCanDeactivateGuard implements CanDeactivate<FormNewComponent> {
canDeactivate(component: FormNewComponent): boolean {
if(component.formNew.dirty) {
return confirm('Are you sure you want to discard your changes?')
}

return true
}
}


Routing file:



export const ROUTES: Routes = [

{ path: '', component: TelaAcessoComponent },

{ path: 'homepage', component: HomepageComponent, children: [
{ path: '', component: QuotesAbertasComponent},
{ path: 'quotes-abertas', component: QuotesAbertasComponent},
{ path: 'quotes-fechadas', component: QuotesFechadasComponent},
{ path: 'quotes-qss', component: QuotesQssComponent},
] },

{
path: 'quotes-abertas/:id',
component: QuotesAbertasDetalhesComponent,
canDeactivate: [QuotesAbertasCanDeactivateGuard]
},

{
path: 'quotes-fechadas/:id',
component: QuotesFechadasDetalhesComponent,
canDeactivate: [QuotesFechadasCanDeactivateGuard]
},

{
path: 'quotes-qss/:id',
component: QuotesQssDetailsComponent,
canDeactivate: [QuotesQssCanDeactivateGuard]
},

{ path: 'formulario', component: FormularioComponent },

{
path: 'formulario-New',
component: FormNewComponent,
canDeactivate: [FormNewCanDeactivateGuard]
},

{ path: 'formulario-Amendment', component: FormAmendmentComponent },
{ path: 'formulario-Migration', component: FormMigrationComponent },
{ path: 'formulario-Renewal/Replacement', component: FormReplacementRenewalComponent },
{
path: 'products/:id',
component: ProductsComponent,
canDeactivate: [ProductsCanDeactivateGuard]
},
]









share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm having trouble making Can Deactivate work in my application. In the file form.component.html I have the following code:



       <div class="form-group">
    <label for="quoteType">Quote Type</label>
    <select class="form-control" id="quoteType" name="QuoteType" #type (change)="getQuoteType(type.value)">
    <option selected="selected">---</option>
    <option value="New">New</option>
    <option value="Amendment">Amendment</option>
    <option value="Migration">Migration</option>
    <option value="Replacement/Renewal">Replacement/Renewal</option>
    </select>
    </div>

    <div [ngSwitch]="quotetype">
    <div *ngSwitchCase="'New'">
    <app-form-new [quotetype]="quotetype"></app-form-new>
    </div>
    <div *ngSwitchCase="'Amendment'">
    <app-form-amendment [quotetype]="quotetype"></app-form-amendment>
    </div>
    <div *ngSwitchCase="'Migration'">
    <app-form-migration [quotetype]="quotetype"></app-form-migration>
    </div>
    <div *ngSwitchCase="'Replacement/Renewal'">
    <app-form-replacement-renewal [quotetype]="quotetype"></app-form-replacement-renewal>
    </div>
    </div>


    Depending on what I select will render a different component in this template. In this case I will select "New"!



    Image before selecting:
    Image 1



    After New is selected:
    Image 2



    What I want is: if the user starts filling out the form and unwittingly or purposely hit the back button to the previous page, a message appears asking if he really wants to do this.



    I can do it on other pages of this system, but when I go to those forms it does not work. Below is the code I have made so far.



    new-can-deactivate-guard.service.ts

    import { Injectable } from '@angular/core'
    import { CanDeactivate } from "@angular/router";
    import { FormNewComponent } from './form-new.component'

    @Injectable({
    providedIn: 'root'
    })
    export class FormNewCanDeactivateGuard implements CanDeactivate<FormNewComponent> {
    canDeactivate(component: FormNewComponent): boolean {
    if(component.formNew.dirty) {
    return confirm('Are you sure you want to discard your changes?')
    }

    return true
    }
    }


    Routing file:



    export const ROUTES: Routes = [

    { path: '', component: TelaAcessoComponent },

    { path: 'homepage', component: HomepageComponent, children: [
    { path: '', component: QuotesAbertasComponent},
    { path: 'quotes-abertas', component: QuotesAbertasComponent},
    { path: 'quotes-fechadas', component: QuotesFechadasComponent},
    { path: 'quotes-qss', component: QuotesQssComponent},
    ] },

    {
    path: 'quotes-abertas/:id',
    component: QuotesAbertasDetalhesComponent,
    canDeactivate: [QuotesAbertasCanDeactivateGuard]
    },

    {
    path: 'quotes-fechadas/:id',
    component: QuotesFechadasDetalhesComponent,
    canDeactivate: [QuotesFechadasCanDeactivateGuard]
    },

    {
    path: 'quotes-qss/:id',
    component: QuotesQssDetailsComponent,
    canDeactivate: [QuotesQssCanDeactivateGuard]
    },

    { path: 'formulario', component: FormularioComponent },

    {
    path: 'formulario-New',
    component: FormNewComponent,
    canDeactivate: [FormNewCanDeactivateGuard]
    },

    { path: 'formulario-Amendment', component: FormAmendmentComponent },
    { path: 'formulario-Migration', component: FormMigrationComponent },
    { path: 'formulario-Renewal/Replacement', component: FormReplacementRenewalComponent },
    {
    path: 'products/:id',
    component: ProductsComponent,
    canDeactivate: [ProductsCanDeactivateGuard]
    },
    ]









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm having trouble making Can Deactivate work in my application. In the file form.component.html I have the following code:



         <div class="form-group">
      <label for="quoteType">Quote Type</label>
      <select class="form-control" id="quoteType" name="QuoteType" #type (change)="getQuoteType(type.value)">
      <option selected="selected">---</option>
      <option value="New">New</option>
      <option value="Amendment">Amendment</option>
      <option value="Migration">Migration</option>
      <option value="Replacement/Renewal">Replacement/Renewal</option>
      </select>
      </div>

      <div [ngSwitch]="quotetype">
      <div *ngSwitchCase="'New'">
      <app-form-new [quotetype]="quotetype"></app-form-new>
      </div>
      <div *ngSwitchCase="'Amendment'">
      <app-form-amendment [quotetype]="quotetype"></app-form-amendment>
      </div>
      <div *ngSwitchCase="'Migration'">
      <app-form-migration [quotetype]="quotetype"></app-form-migration>
      </div>
      <div *ngSwitchCase="'Replacement/Renewal'">
      <app-form-replacement-renewal [quotetype]="quotetype"></app-form-replacement-renewal>
      </div>
      </div>


      Depending on what I select will render a different component in this template. In this case I will select "New"!



      Image before selecting:
      Image 1



      After New is selected:
      Image 2



      What I want is: if the user starts filling out the form and unwittingly or purposely hit the back button to the previous page, a message appears asking if he really wants to do this.



      I can do it on other pages of this system, but when I go to those forms it does not work. Below is the code I have made so far.



      new-can-deactivate-guard.service.ts

      import { Injectable } from '@angular/core'
      import { CanDeactivate } from "@angular/router";
      import { FormNewComponent } from './form-new.component'

      @Injectable({
      providedIn: 'root'
      })
      export class FormNewCanDeactivateGuard implements CanDeactivate<FormNewComponent> {
      canDeactivate(component: FormNewComponent): boolean {
      if(component.formNew.dirty) {
      return confirm('Are you sure you want to discard your changes?')
      }

      return true
      }
      }


      Routing file:



      export const ROUTES: Routes = [

      { path: '', component: TelaAcessoComponent },

      { path: 'homepage', component: HomepageComponent, children: [
      { path: '', component: QuotesAbertasComponent},
      { path: 'quotes-abertas', component: QuotesAbertasComponent},
      { path: 'quotes-fechadas', component: QuotesFechadasComponent},
      { path: 'quotes-qss', component: QuotesQssComponent},
      ] },

      {
      path: 'quotes-abertas/:id',
      component: QuotesAbertasDetalhesComponent,
      canDeactivate: [QuotesAbertasCanDeactivateGuard]
      },

      {
      path: 'quotes-fechadas/:id',
      component: QuotesFechadasDetalhesComponent,
      canDeactivate: [QuotesFechadasCanDeactivateGuard]
      },

      {
      path: 'quotes-qss/:id',
      component: QuotesQssDetailsComponent,
      canDeactivate: [QuotesQssCanDeactivateGuard]
      },

      { path: 'formulario', component: FormularioComponent },

      {
      path: 'formulario-New',
      component: FormNewComponent,
      canDeactivate: [FormNewCanDeactivateGuard]
      },

      { path: 'formulario-Amendment', component: FormAmendmentComponent },
      { path: 'formulario-Migration', component: FormMigrationComponent },
      { path: 'formulario-Renewal/Replacement', component: FormReplacementRenewalComponent },
      {
      path: 'products/:id',
      component: ProductsComponent,
      canDeactivate: [ProductsCanDeactivateGuard]
      },
      ]









      share|improve this question















      I'm having trouble making Can Deactivate work in my application. In the file form.component.html I have the following code:



         <div class="form-group">
      <label for="quoteType">Quote Type</label>
      <select class="form-control" id="quoteType" name="QuoteType" #type (change)="getQuoteType(type.value)">
      <option selected="selected">---</option>
      <option value="New">New</option>
      <option value="Amendment">Amendment</option>
      <option value="Migration">Migration</option>
      <option value="Replacement/Renewal">Replacement/Renewal</option>
      </select>
      </div>

      <div [ngSwitch]="quotetype">
      <div *ngSwitchCase="'New'">
      <app-form-new [quotetype]="quotetype"></app-form-new>
      </div>
      <div *ngSwitchCase="'Amendment'">
      <app-form-amendment [quotetype]="quotetype"></app-form-amendment>
      </div>
      <div *ngSwitchCase="'Migration'">
      <app-form-migration [quotetype]="quotetype"></app-form-migration>
      </div>
      <div *ngSwitchCase="'Replacement/Renewal'">
      <app-form-replacement-renewal [quotetype]="quotetype"></app-form-replacement-renewal>
      </div>
      </div>


      Depending on what I select will render a different component in this template. In this case I will select "New"!



      Image before selecting:
      Image 1



      After New is selected:
      Image 2



      What I want is: if the user starts filling out the form and unwittingly or purposely hit the back button to the previous page, a message appears asking if he really wants to do this.



      I can do it on other pages of this system, but when I go to those forms it does not work. Below is the code I have made so far.



      new-can-deactivate-guard.service.ts

      import { Injectable } from '@angular/core'
      import { CanDeactivate } from "@angular/router";
      import { FormNewComponent } from './form-new.component'

      @Injectable({
      providedIn: 'root'
      })
      export class FormNewCanDeactivateGuard implements CanDeactivate<FormNewComponent> {
      canDeactivate(component: FormNewComponent): boolean {
      if(component.formNew.dirty) {
      return confirm('Are you sure you want to discard your changes?')
      }

      return true
      }
      }


      Routing file:



      export const ROUTES: Routes = [

      { path: '', component: TelaAcessoComponent },

      { path: 'homepage', component: HomepageComponent, children: [
      { path: '', component: QuotesAbertasComponent},
      { path: 'quotes-abertas', component: QuotesAbertasComponent},
      { path: 'quotes-fechadas', component: QuotesFechadasComponent},
      { path: 'quotes-qss', component: QuotesQssComponent},
      ] },

      {
      path: 'quotes-abertas/:id',
      component: QuotesAbertasDetalhesComponent,
      canDeactivate: [QuotesAbertasCanDeactivateGuard]
      },

      {
      path: 'quotes-fechadas/:id',
      component: QuotesFechadasDetalhesComponent,
      canDeactivate: [QuotesFechadasCanDeactivateGuard]
      },

      {
      path: 'quotes-qss/:id',
      component: QuotesQssDetailsComponent,
      canDeactivate: [QuotesQssCanDeactivateGuard]
      },

      { path: 'formulario', component: FormularioComponent },

      {
      path: 'formulario-New',
      component: FormNewComponent,
      canDeactivate: [FormNewCanDeactivateGuard]
      },

      { path: 'formulario-Amendment', component: FormAmendmentComponent },
      { path: 'formulario-Migration', component: FormMigrationComponent },
      { path: 'formulario-Renewal/Replacement', component: FormReplacementRenewalComponent },
      {
      path: 'products/:id',
      component: ProductsComponent,
      canDeactivate: [ProductsCanDeactivateGuard]
      },
      ]






      angular routes angular-components feedback angular-route-guards






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 13:29









      SiddAjmera

      9,72621037




      9,72621037










      asked Nov 8 at 13:20









      Leonardo Vinicius

      12




      12





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208609%2fcan-deactivate-guard-does-not-work-when-used-on-one-component-that-is-inside-ano%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208609%2fcan-deactivate-guard-does-not-work-when-used-on-one-component-that-is-inside-ano%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff