Conditional option selection from event on angular x












0















I have a Select list and I want to launch an action (event) after the user clicks on a choice (Option).



I used (change) event on the Select element, it works but it updated the selected item. there is not an event that can be launched on the click of an option?



The action I want is if the user clicks on an option I do a test if it's true I allow him to select this item, otherwise he stays on the current.



the current code :



<select id="myselect" (change)="verifyData($event.target.value)" formControlName="myElement">
<option *ngFor="let items of items" [value]="item.name">{{ item.name }}</option>
</select>









share|improve this question





























    0















    I have a Select list and I want to launch an action (event) after the user clicks on a choice (Option).



    I used (change) event on the Select element, it works but it updated the selected item. there is not an event that can be launched on the click of an option?



    The action I want is if the user clicks on an option I do a test if it's true I allow him to select this item, otherwise he stays on the current.



    the current code :



    <select id="myselect" (change)="verifyData($event.target.value)" formControlName="myElement">
    <option *ngFor="let items of items" [value]="item.name">{{ item.name }}</option>
    </select>









    share|improve this question



























      0












      0








      0








      I have a Select list and I want to launch an action (event) after the user clicks on a choice (Option).



      I used (change) event on the Select element, it works but it updated the selected item. there is not an event that can be launched on the click of an option?



      The action I want is if the user clicks on an option I do a test if it's true I allow him to select this item, otherwise he stays on the current.



      the current code :



      <select id="myselect" (change)="verifyData($event.target.value)" formControlName="myElement">
      <option *ngFor="let items of items" [value]="item.name">{{ item.name }}</option>
      </select>









      share|improve this question
















      I have a Select list and I want to launch an action (event) after the user clicks on a choice (Option).



      I used (change) event on the Select element, it works but it updated the selected item. there is not an event that can be launched on the click of an option?



      The action I want is if the user clicks on an option I do a test if it's true I allow him to select this item, otherwise he stays on the current.



      the current code :



      <select id="myselect" (change)="verifyData($event.target.value)" formControlName="myElement">
      <option *ngFor="let items of items" [value]="item.name">{{ item.name }}</option>
      </select>






      angular typescript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 22:33









      Adam Genshaft

      31829




      31829










      asked Jan 2 at 18:13









      AbadouAbadou

      104




      104
























          1 Answer
          1






          active

          oldest

          votes


















          0














          EDITED



          You can subscribe to value changes of the form control and set it back to previous value if some logic meets your needs. This should work:



          ngOnInit(item) {
          this.oldValue = this.form.value.myElement;
          this.form.get('myElement').valueChanges.subscribe((a) => {
          if (a == 3) {
          this.form.get('myElement').setValue(this.oldValue, { emitEvent: false });
          } else {
          this.oldValue = a;
          }
          });
          }


          Here's a link to a working example on stackblitz
          https://stackblitz.com/edit/angular-conditional-select-change?file=src%2Fapp%2Fapp.component.ts






          share|improve this answer


























          • in angular the event click not working in option element, you can test it !! it not works !!

            – Abadou
            Jan 2 at 19:20











          • My mistake, I completely edited the answer and added a working example. Also I suggested a change of the question title. Hope it will help :)

            – Adam Genshaft
            Jan 2 at 21:40











          • Thank you, i will try this approch for my use case :)

            – Abadou
            Jan 2 at 21:48











          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',
          autoActivateHeartbeat: false,
          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%2f54011195%2fconditional-option-selection-from-event-on-angular-x%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









          0














          EDITED



          You can subscribe to value changes of the form control and set it back to previous value if some logic meets your needs. This should work:



          ngOnInit(item) {
          this.oldValue = this.form.value.myElement;
          this.form.get('myElement').valueChanges.subscribe((a) => {
          if (a == 3) {
          this.form.get('myElement').setValue(this.oldValue, { emitEvent: false });
          } else {
          this.oldValue = a;
          }
          });
          }


          Here's a link to a working example on stackblitz
          https://stackblitz.com/edit/angular-conditional-select-change?file=src%2Fapp%2Fapp.component.ts






          share|improve this answer


























          • in angular the event click not working in option element, you can test it !! it not works !!

            – Abadou
            Jan 2 at 19:20











          • My mistake, I completely edited the answer and added a working example. Also I suggested a change of the question title. Hope it will help :)

            – Adam Genshaft
            Jan 2 at 21:40











          • Thank you, i will try this approch for my use case :)

            – Abadou
            Jan 2 at 21:48
















          0














          EDITED



          You can subscribe to value changes of the form control and set it back to previous value if some logic meets your needs. This should work:



          ngOnInit(item) {
          this.oldValue = this.form.value.myElement;
          this.form.get('myElement').valueChanges.subscribe((a) => {
          if (a == 3) {
          this.form.get('myElement').setValue(this.oldValue, { emitEvent: false });
          } else {
          this.oldValue = a;
          }
          });
          }


          Here's a link to a working example on stackblitz
          https://stackblitz.com/edit/angular-conditional-select-change?file=src%2Fapp%2Fapp.component.ts






          share|improve this answer


























          • in angular the event click not working in option element, you can test it !! it not works !!

            – Abadou
            Jan 2 at 19:20











          • My mistake, I completely edited the answer and added a working example. Also I suggested a change of the question title. Hope it will help :)

            – Adam Genshaft
            Jan 2 at 21:40











          • Thank you, i will try this approch for my use case :)

            – Abadou
            Jan 2 at 21:48














          0












          0








          0







          EDITED



          You can subscribe to value changes of the form control and set it back to previous value if some logic meets your needs. This should work:



          ngOnInit(item) {
          this.oldValue = this.form.value.myElement;
          this.form.get('myElement').valueChanges.subscribe((a) => {
          if (a == 3) {
          this.form.get('myElement').setValue(this.oldValue, { emitEvent: false });
          } else {
          this.oldValue = a;
          }
          });
          }


          Here's a link to a working example on stackblitz
          https://stackblitz.com/edit/angular-conditional-select-change?file=src%2Fapp%2Fapp.component.ts






          share|improve this answer















          EDITED



          You can subscribe to value changes of the form control and set it back to previous value if some logic meets your needs. This should work:



          ngOnInit(item) {
          this.oldValue = this.form.value.myElement;
          this.form.get('myElement').valueChanges.subscribe((a) => {
          if (a == 3) {
          this.form.get('myElement').setValue(this.oldValue, { emitEvent: false });
          } else {
          this.oldValue = a;
          }
          });
          }


          Here's a link to a working example on stackblitz
          https://stackblitz.com/edit/angular-conditional-select-change?file=src%2Fapp%2Fapp.component.ts







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 21:36

























          answered Jan 2 at 18:41









          Adam GenshaftAdam Genshaft

          31829




          31829













          • in angular the event click not working in option element, you can test it !! it not works !!

            – Abadou
            Jan 2 at 19:20











          • My mistake, I completely edited the answer and added a working example. Also I suggested a change of the question title. Hope it will help :)

            – Adam Genshaft
            Jan 2 at 21:40











          • Thank you, i will try this approch for my use case :)

            – Abadou
            Jan 2 at 21:48



















          • in angular the event click not working in option element, you can test it !! it not works !!

            – Abadou
            Jan 2 at 19:20











          • My mistake, I completely edited the answer and added a working example. Also I suggested a change of the question title. Hope it will help :)

            – Adam Genshaft
            Jan 2 at 21:40











          • Thank you, i will try this approch for my use case :)

            – Abadou
            Jan 2 at 21:48

















          in angular the event click not working in option element, you can test it !! it not works !!

          – Abadou
          Jan 2 at 19:20





          in angular the event click not working in option element, you can test it !! it not works !!

          – Abadou
          Jan 2 at 19:20













          My mistake, I completely edited the answer and added a working example. Also I suggested a change of the question title. Hope it will help :)

          – Adam Genshaft
          Jan 2 at 21:40





          My mistake, I completely edited the answer and added a working example. Also I suggested a change of the question title. Hope it will help :)

          – Adam Genshaft
          Jan 2 at 21:40













          Thank you, i will try this approch for my use case :)

          – Abadou
          Jan 2 at 21:48





          Thank you, i will try this approch for my use case :)

          – Abadou
          Jan 2 at 21:48




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011195%2fconditional-option-selection-from-event-on-angular-x%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

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas