Angular 6 Fallback/Redirect if param is undefined











up vote
2
down vote

favorite












I'd like to have routes set like this:



Base Route: /events/:date



A click event redirects to /events, then the routes file would have to fallback to /events/moment().format('YYYY-MM-DD') to render EventsComponent.



Inside EventsComponent you have an action to change date, which navigates according to the day you selected, in this case the routes file wouldn't need to fallback, since the :date is defined.



I just don't know how to apply it to my needs.

Thanks










share|improve this question


























    up vote
    2
    down vote

    favorite












    I'd like to have routes set like this:



    Base Route: /events/:date



    A click event redirects to /events, then the routes file would have to fallback to /events/moment().format('YYYY-MM-DD') to render EventsComponent.



    Inside EventsComponent you have an action to change date, which navigates according to the day you selected, in this case the routes file wouldn't need to fallback, since the :date is defined.



    I just don't know how to apply it to my needs.

    Thanks










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'd like to have routes set like this:



      Base Route: /events/:date



      A click event redirects to /events, then the routes file would have to fallback to /events/moment().format('YYYY-MM-DD') to render EventsComponent.



      Inside EventsComponent you have an action to change date, which navigates according to the day you selected, in this case the routes file wouldn't need to fallback, since the :date is defined.



      I just don't know how to apply it to my needs.

      Thanks










      share|improve this question













      I'd like to have routes set like this:



      Base Route: /events/:date



      A click event redirects to /events, then the routes file would have to fallback to /events/moment().format('YYYY-MM-DD') to render EventsComponent.



      Inside EventsComponent you have an action to change date, which navigates according to the day you selected, in this case the routes file wouldn't need to fallback, since the :date is defined.



      I just don't know how to apply it to my needs.

      Thanks







      angular angular-router






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 16:54









      João Ghignatti

      18911




      18911
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          If you just need to avoid navigation when the date parameter is not defined then you should have 2 routes and add a canActivate guard to the second:



          {
          path: 'events',
          component: EventsComponent
          },
          {
          path: 'events/:date',
          component: EventsDetailComponent,
          canActivate: EventDefinedGuard
          }


          where EventDefinedGuard is a router guard that check for the param to be defined, otherwise redirects.






          share|improve this answer





















          • Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a :date, if it's not defined, to render EventsComponent, but if it is defined I just need to render EventsComponent. I need that cause the part of my app that redirects to events cannot be changed right now to force a "first value" of moment().format('YYYY-MM-DD'). I can only change the action part inside EventsComponent to change the date selected.
            – João Ghignatti
            Nov 9 at 17:35










          • Ok so you have only one route and need to make sure there's always a parameter when going to /events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.
            – Massimiliano Sartoretto
            Nov 9 at 17:43










          • Do you mind editing your answer with updated code so I can accept it?
            – João Ghignatti
            Nov 17 at 15:08











          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%2f53230135%2fangular-6-fallback-redirect-if-param-is-undefined%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          If you just need to avoid navigation when the date parameter is not defined then you should have 2 routes and add a canActivate guard to the second:



          {
          path: 'events',
          component: EventsComponent
          },
          {
          path: 'events/:date',
          component: EventsDetailComponent,
          canActivate: EventDefinedGuard
          }


          where EventDefinedGuard is a router guard that check for the param to be defined, otherwise redirects.






          share|improve this answer





















          • Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a :date, if it's not defined, to render EventsComponent, but if it is defined I just need to render EventsComponent. I need that cause the part of my app that redirects to events cannot be changed right now to force a "first value" of moment().format('YYYY-MM-DD'). I can only change the action part inside EventsComponent to change the date selected.
            – João Ghignatti
            Nov 9 at 17:35










          • Ok so you have only one route and need to make sure there's always a parameter when going to /events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.
            – Massimiliano Sartoretto
            Nov 9 at 17:43










          • Do you mind editing your answer with updated code so I can accept it?
            – João Ghignatti
            Nov 17 at 15:08















          up vote
          0
          down vote













          If you just need to avoid navigation when the date parameter is not defined then you should have 2 routes and add a canActivate guard to the second:



          {
          path: 'events',
          component: EventsComponent
          },
          {
          path: 'events/:date',
          component: EventsDetailComponent,
          canActivate: EventDefinedGuard
          }


          where EventDefinedGuard is a router guard that check for the param to be defined, otherwise redirects.






          share|improve this answer





















          • Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a :date, if it's not defined, to render EventsComponent, but if it is defined I just need to render EventsComponent. I need that cause the part of my app that redirects to events cannot be changed right now to force a "first value" of moment().format('YYYY-MM-DD'). I can only change the action part inside EventsComponent to change the date selected.
            – João Ghignatti
            Nov 9 at 17:35










          • Ok so you have only one route and need to make sure there's always a parameter when going to /events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.
            – Massimiliano Sartoretto
            Nov 9 at 17:43










          • Do you mind editing your answer with updated code so I can accept it?
            – João Ghignatti
            Nov 17 at 15:08













          up vote
          0
          down vote










          up vote
          0
          down vote









          If you just need to avoid navigation when the date parameter is not defined then you should have 2 routes and add a canActivate guard to the second:



          {
          path: 'events',
          component: EventsComponent
          },
          {
          path: 'events/:date',
          component: EventsDetailComponent,
          canActivate: EventDefinedGuard
          }


          where EventDefinedGuard is a router guard that check for the param to be defined, otherwise redirects.






          share|improve this answer












          If you just need to avoid navigation when the date parameter is not defined then you should have 2 routes and add a canActivate guard to the second:



          {
          path: 'events',
          component: EventsComponent
          },
          {
          path: 'events/:date',
          component: EventsDetailComponent,
          canActivate: EventDefinedGuard
          }


          where EventDefinedGuard is a router guard that check for the param to be defined, otherwise redirects.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 17:29









          Massimiliano Sartoretto

          1818




          1818












          • Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a :date, if it's not defined, to render EventsComponent, but if it is defined I just need to render EventsComponent. I need that cause the part of my app that redirects to events cannot be changed right now to force a "first value" of moment().format('YYYY-MM-DD'). I can only change the action part inside EventsComponent to change the date selected.
            – João Ghignatti
            Nov 9 at 17:35










          • Ok so you have only one route and need to make sure there's always a parameter when going to /events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.
            – Massimiliano Sartoretto
            Nov 9 at 17:43










          • Do you mind editing your answer with updated code so I can accept it?
            – João Ghignatti
            Nov 17 at 15:08


















          • Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a :date, if it's not defined, to render EventsComponent, but if it is defined I just need to render EventsComponent. I need that cause the part of my app that redirects to events cannot be changed right now to force a "first value" of moment().format('YYYY-MM-DD'). I can only change the action part inside EventsComponent to change the date selected.
            – João Ghignatti
            Nov 9 at 17:35










          • Ok so you have only one route and need to make sure there's always a parameter when going to /events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.
            – Massimiliano Sartoretto
            Nov 9 at 17:43










          • Do you mind editing your answer with updated code so I can accept it?
            – João Ghignatti
            Nov 17 at 15:08
















          Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a :date, if it's not defined, to render EventsComponent, but if it is defined I just need to render EventsComponent. I need that cause the part of my app that redirects to events cannot be changed right now to force a "first value" of moment().format('YYYY-MM-DD'). I can only change the action part inside EventsComponent to change the date selected.
          – João Ghignatti
          Nov 9 at 17:35




          Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a :date, if it's not defined, to render EventsComponent, but if it is defined I just need to render EventsComponent. I need that cause the part of my app that redirects to events cannot be changed right now to force a "first value" of moment().format('YYYY-MM-DD'). I can only change the action part inside EventsComponent to change the date selected.
          – João Ghignatti
          Nov 9 at 17:35












          Ok so you have only one route and need to make sure there's always a parameter when going to /events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.
          – Massimiliano Sartoretto
          Nov 9 at 17:43




          Ok so you have only one route and need to make sure there's always a parameter when going to /events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.
          – Massimiliano Sartoretto
          Nov 9 at 17:43












          Do you mind editing your answer with updated code so I can accept it?
          – João Ghignatti
          Nov 17 at 15:08




          Do you mind editing your answer with updated code so I can accept it?
          – João Ghignatti
          Nov 17 at 15:08


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230135%2fangular-6-fallback-redirect-if-param-is-undefined%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

          How to pass form data using jquery Ajax to insert data in database?

          National Museum of Racing and Hall of Fame

          Guess what letter conforming each word