Pass data on parent change angular 7












1















I have two dropdown list and I need to pass both of their values to updateGraph function in the component when either one of the two changes



   <div class="card-body" (change)="updateGraph($event)">
<div class="row">
<label>City</label>
<select data-id="selects1" class="browser-default custom-select">
<label>City</label>
<option selected>Select a city</option>
<option *ngFor="let city of cities" [value]="city">{{ city }} </option>
</select>
</div>
<div class="row">
<label>Scale</label>
<select data-id="selects" class="browser-default custom-select">
<option selected>Scale</option>
<option *ngFor="let scale of scales" [ngValue]="scale.value">{{ scale.Name }}
</option>
</select>
</div>
<div class="row">
<mdb-date-picker name="mydate" [options]="myDatePickerOptions" [placeholder]="'Selected date'"
[(ngModel)]="model" required></mdb-date-picker>
</div>
</div>









share|improve this question





























    1















    I have two dropdown list and I need to pass both of their values to updateGraph function in the component when either one of the two changes



       <div class="card-body" (change)="updateGraph($event)">
    <div class="row">
    <label>City</label>
    <select data-id="selects1" class="browser-default custom-select">
    <label>City</label>
    <option selected>Select a city</option>
    <option *ngFor="let city of cities" [value]="city">{{ city }} </option>
    </select>
    </div>
    <div class="row">
    <label>Scale</label>
    <select data-id="selects" class="browser-default custom-select">
    <option selected>Scale</option>
    <option *ngFor="let scale of scales" [ngValue]="scale.value">{{ scale.Name }}
    </option>
    </select>
    </div>
    <div class="row">
    <mdb-date-picker name="mydate" [options]="myDatePickerOptions" [placeholder]="'Selected date'"
    [(ngModel)]="model" required></mdb-date-picker>
    </div>
    </div>









    share|improve this question



























      1












      1








      1








      I have two dropdown list and I need to pass both of their values to updateGraph function in the component when either one of the two changes



         <div class="card-body" (change)="updateGraph($event)">
      <div class="row">
      <label>City</label>
      <select data-id="selects1" class="browser-default custom-select">
      <label>City</label>
      <option selected>Select a city</option>
      <option *ngFor="let city of cities" [value]="city">{{ city }} </option>
      </select>
      </div>
      <div class="row">
      <label>Scale</label>
      <select data-id="selects" class="browser-default custom-select">
      <option selected>Scale</option>
      <option *ngFor="let scale of scales" [ngValue]="scale.value">{{ scale.Name }}
      </option>
      </select>
      </div>
      <div class="row">
      <mdb-date-picker name="mydate" [options]="myDatePickerOptions" [placeholder]="'Selected date'"
      [(ngModel)]="model" required></mdb-date-picker>
      </div>
      </div>









      share|improve this question
















      I have two dropdown list and I need to pass both of their values to updateGraph function in the component when either one of the two changes



         <div class="card-body" (change)="updateGraph($event)">
      <div class="row">
      <label>City</label>
      <select data-id="selects1" class="browser-default custom-select">
      <label>City</label>
      <option selected>Select a city</option>
      <option *ngFor="let city of cities" [value]="city">{{ city }} </option>
      </select>
      </div>
      <div class="row">
      <label>Scale</label>
      <select data-id="selects" class="browser-default custom-select">
      <option selected>Scale</option>
      <option *ngFor="let scale of scales" [ngValue]="scale.value">{{ scale.Name }}
      </option>
      </select>
      </div>
      <div class="row">
      <mdb-date-picker name="mydate" [options]="myDatePickerOptions" [placeholder]="'Selected date'"
      [(ngModel)]="model" required></mdb-date-picker>
      </div>
      </div>






      angular angular7






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 18:49









      Goncalo Peres

      1,3881419




      1,3881419










      asked Nov 13 '18 at 0:52









      Christian BawmanChristian Bawman

      235




      235
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You have to apply to updateGraph on both select elements like below



          <select data-id="selects1" (change)="updateGraph($event.target.value)"> class="browser-default custom-select">
          <label>City</label>
          <option selected>Select a city</option>
          <option *ngFor="let city of cities" [value]="city">{{ city }} </option>
          </select>


          If you want to bind a property like id as value



          <option *ngFor="let city of cities" [value]="city.id">


          So $event.target.value will be the selected id



          if you want to reuse the same method for both dropdown , just pass some constantvalue as a parameter



           updateGraph('scale',$event.target.value)  //for scale 
          updateGraph('city',$event.target.value) //city


          from the component you can check a condition



          updateGraph(type,value){
          if(type==='scale'){
          //value for scale
          }
          else if(type==='city'){
          //value for city
          }
          }


          Stackblitz Demo






          share|improve this answer


























          • I understand the concept and it works like this but the updateGraph function its making a call to a service and the service is calling up an API but this API needs to receive two parameters. ok lets say that I have already selected a city but now i go and i select an scale from here I can get the scale value, but how do i get the city that i previously selected on that event?

            – Christian Bawman
            Nov 13 '18 at 2:38











          • @ChristianBawman you can create a separate property for holding the selectedValue for both city and scale in the component like in the stackblitz demo. So the value of the property will be always there. If you are not clear the idea I can update the stackblitz by adding one more dropdown

            – Code-EZ
            Nov 13 '18 at 2:42













          • @ChristianBawman Updated the demo application. Please let me know if you have any question

            – Code-EZ
            Nov 13 '18 at 2:48











          • just AMAZING, it worked. I'm not using a button, instead i'm calling directly the function updateGraph and with a few validations I was able to get the values in that function, thank you very so much sir !

            – Christian Bawman
            Nov 13 '18 at 3:36



















          0














          I think the issue is with



          (change)="updateGraph($event)"


          being placed on a div element, as divs do not have onChange events, unless the card-body is somehow providing that.



          I would move that call to both of the select elements, so it will be triggered on either of the select's onChange events.



          You can also pass in whatever variable you need to, or just reference them from the .ts file.



          Option #1 looks like it would be, moving it to the selects and then using:



          (change)="updateGraph(city, scale.Name)"


          Option #2 on the .ts file you could just reference this.city and this.scale.Name in your updateGraph function.



          But make sure the (change) is on both of the select elements






          share|improve this answer





















          • 1





            I don't have nothing called 'state' actually on my code and well the scale I do have it but when I do a console log in the function it says that cannot read the property Name.

            – Christian Bawman
            Nov 13 '18 at 1:09











          • sorry i meant city!

            – JBoothUA
            Nov 13 '18 at 3:20











          • PS. unless you're planning on using $event you dont have to use it. but you can use it if you do not have access to the binded model. hope that makes sense

            – JBoothUA
            Nov 13 '18 at 3:22











          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%2f53272223%2fpass-data-on-parent-change-angular-7%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You have to apply to updateGraph on both select elements like below



          <select data-id="selects1" (change)="updateGraph($event.target.value)"> class="browser-default custom-select">
          <label>City</label>
          <option selected>Select a city</option>
          <option *ngFor="let city of cities" [value]="city">{{ city }} </option>
          </select>


          If you want to bind a property like id as value



          <option *ngFor="let city of cities" [value]="city.id">


          So $event.target.value will be the selected id



          if you want to reuse the same method for both dropdown , just pass some constantvalue as a parameter



           updateGraph('scale',$event.target.value)  //for scale 
          updateGraph('city',$event.target.value) //city


          from the component you can check a condition



          updateGraph(type,value){
          if(type==='scale'){
          //value for scale
          }
          else if(type==='city'){
          //value for city
          }
          }


          Stackblitz Demo






          share|improve this answer


























          • I understand the concept and it works like this but the updateGraph function its making a call to a service and the service is calling up an API but this API needs to receive two parameters. ok lets say that I have already selected a city but now i go and i select an scale from here I can get the scale value, but how do i get the city that i previously selected on that event?

            – Christian Bawman
            Nov 13 '18 at 2:38











          • @ChristianBawman you can create a separate property for holding the selectedValue for both city and scale in the component like in the stackblitz demo. So the value of the property will be always there. If you are not clear the idea I can update the stackblitz by adding one more dropdown

            – Code-EZ
            Nov 13 '18 at 2:42













          • @ChristianBawman Updated the demo application. Please let me know if you have any question

            – Code-EZ
            Nov 13 '18 at 2:48











          • just AMAZING, it worked. I'm not using a button, instead i'm calling directly the function updateGraph and with a few validations I was able to get the values in that function, thank you very so much sir !

            – Christian Bawman
            Nov 13 '18 at 3:36
















          0














          You have to apply to updateGraph on both select elements like below



          <select data-id="selects1" (change)="updateGraph($event.target.value)"> class="browser-default custom-select">
          <label>City</label>
          <option selected>Select a city</option>
          <option *ngFor="let city of cities" [value]="city">{{ city }} </option>
          </select>


          If you want to bind a property like id as value



          <option *ngFor="let city of cities" [value]="city.id">


          So $event.target.value will be the selected id



          if you want to reuse the same method for both dropdown , just pass some constantvalue as a parameter



           updateGraph('scale',$event.target.value)  //for scale 
          updateGraph('city',$event.target.value) //city


          from the component you can check a condition



          updateGraph(type,value){
          if(type==='scale'){
          //value for scale
          }
          else if(type==='city'){
          //value for city
          }
          }


          Stackblitz Demo






          share|improve this answer


























          • I understand the concept and it works like this but the updateGraph function its making a call to a service and the service is calling up an API but this API needs to receive two parameters. ok lets say that I have already selected a city but now i go and i select an scale from here I can get the scale value, but how do i get the city that i previously selected on that event?

            – Christian Bawman
            Nov 13 '18 at 2:38











          • @ChristianBawman you can create a separate property for holding the selectedValue for both city and scale in the component like in the stackblitz demo. So the value of the property will be always there. If you are not clear the idea I can update the stackblitz by adding one more dropdown

            – Code-EZ
            Nov 13 '18 at 2:42













          • @ChristianBawman Updated the demo application. Please let me know if you have any question

            – Code-EZ
            Nov 13 '18 at 2:48











          • just AMAZING, it worked. I'm not using a button, instead i'm calling directly the function updateGraph and with a few validations I was able to get the values in that function, thank you very so much sir !

            – Christian Bawman
            Nov 13 '18 at 3:36














          0












          0








          0







          You have to apply to updateGraph on both select elements like below



          <select data-id="selects1" (change)="updateGraph($event.target.value)"> class="browser-default custom-select">
          <label>City</label>
          <option selected>Select a city</option>
          <option *ngFor="let city of cities" [value]="city">{{ city }} </option>
          </select>


          If you want to bind a property like id as value



          <option *ngFor="let city of cities" [value]="city.id">


          So $event.target.value will be the selected id



          if you want to reuse the same method for both dropdown , just pass some constantvalue as a parameter



           updateGraph('scale',$event.target.value)  //for scale 
          updateGraph('city',$event.target.value) //city


          from the component you can check a condition



          updateGraph(type,value){
          if(type==='scale'){
          //value for scale
          }
          else if(type==='city'){
          //value for city
          }
          }


          Stackblitz Demo






          share|improve this answer















          You have to apply to updateGraph on both select elements like below



          <select data-id="selects1" (change)="updateGraph($event.target.value)"> class="browser-default custom-select">
          <label>City</label>
          <option selected>Select a city</option>
          <option *ngFor="let city of cities" [value]="city">{{ city }} </option>
          </select>


          If you want to bind a property like id as value



          <option *ngFor="let city of cities" [value]="city.id">


          So $event.target.value will be the selected id



          if you want to reuse the same method for both dropdown , just pass some constantvalue as a parameter



           updateGraph('scale',$event.target.value)  //for scale 
          updateGraph('city',$event.target.value) //city


          from the component you can check a condition



          updateGraph(type,value){
          if(type==='scale'){
          //value for scale
          }
          else if(type==='city'){
          //value for city
          }
          }


          Stackblitz Demo







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 14:55

























          answered Nov 13 '18 at 2:17









          Code-EZCode-EZ

          3,36583052




          3,36583052













          • I understand the concept and it works like this but the updateGraph function its making a call to a service and the service is calling up an API but this API needs to receive two parameters. ok lets say that I have already selected a city but now i go and i select an scale from here I can get the scale value, but how do i get the city that i previously selected on that event?

            – Christian Bawman
            Nov 13 '18 at 2:38











          • @ChristianBawman you can create a separate property for holding the selectedValue for both city and scale in the component like in the stackblitz demo. So the value of the property will be always there. If you are not clear the idea I can update the stackblitz by adding one more dropdown

            – Code-EZ
            Nov 13 '18 at 2:42













          • @ChristianBawman Updated the demo application. Please let me know if you have any question

            – Code-EZ
            Nov 13 '18 at 2:48











          • just AMAZING, it worked. I'm not using a button, instead i'm calling directly the function updateGraph and with a few validations I was able to get the values in that function, thank you very so much sir !

            – Christian Bawman
            Nov 13 '18 at 3:36



















          • I understand the concept and it works like this but the updateGraph function its making a call to a service and the service is calling up an API but this API needs to receive two parameters. ok lets say that I have already selected a city but now i go and i select an scale from here I can get the scale value, but how do i get the city that i previously selected on that event?

            – Christian Bawman
            Nov 13 '18 at 2:38











          • @ChristianBawman you can create a separate property for holding the selectedValue for both city and scale in the component like in the stackblitz demo. So the value of the property will be always there. If you are not clear the idea I can update the stackblitz by adding one more dropdown

            – Code-EZ
            Nov 13 '18 at 2:42













          • @ChristianBawman Updated the demo application. Please let me know if you have any question

            – Code-EZ
            Nov 13 '18 at 2:48











          • just AMAZING, it worked. I'm not using a button, instead i'm calling directly the function updateGraph and with a few validations I was able to get the values in that function, thank you very so much sir !

            – Christian Bawman
            Nov 13 '18 at 3:36

















          I understand the concept and it works like this but the updateGraph function its making a call to a service and the service is calling up an API but this API needs to receive two parameters. ok lets say that I have already selected a city but now i go and i select an scale from here I can get the scale value, but how do i get the city that i previously selected on that event?

          – Christian Bawman
          Nov 13 '18 at 2:38





          I understand the concept and it works like this but the updateGraph function its making a call to a service and the service is calling up an API but this API needs to receive two parameters. ok lets say that I have already selected a city but now i go and i select an scale from here I can get the scale value, but how do i get the city that i previously selected on that event?

          – Christian Bawman
          Nov 13 '18 at 2:38













          @ChristianBawman you can create a separate property for holding the selectedValue for both city and scale in the component like in the stackblitz demo. So the value of the property will be always there. If you are not clear the idea I can update the stackblitz by adding one more dropdown

          – Code-EZ
          Nov 13 '18 at 2:42







          @ChristianBawman you can create a separate property for holding the selectedValue for both city and scale in the component like in the stackblitz demo. So the value of the property will be always there. If you are not clear the idea I can update the stackblitz by adding one more dropdown

          – Code-EZ
          Nov 13 '18 at 2:42















          @ChristianBawman Updated the demo application. Please let me know if you have any question

          – Code-EZ
          Nov 13 '18 at 2:48





          @ChristianBawman Updated the demo application. Please let me know if you have any question

          – Code-EZ
          Nov 13 '18 at 2:48













          just AMAZING, it worked. I'm not using a button, instead i'm calling directly the function updateGraph and with a few validations I was able to get the values in that function, thank you very so much sir !

          – Christian Bawman
          Nov 13 '18 at 3:36





          just AMAZING, it worked. I'm not using a button, instead i'm calling directly the function updateGraph and with a few validations I was able to get the values in that function, thank you very so much sir !

          – Christian Bawman
          Nov 13 '18 at 3:36













          0














          I think the issue is with



          (change)="updateGraph($event)"


          being placed on a div element, as divs do not have onChange events, unless the card-body is somehow providing that.



          I would move that call to both of the select elements, so it will be triggered on either of the select's onChange events.



          You can also pass in whatever variable you need to, or just reference them from the .ts file.



          Option #1 looks like it would be, moving it to the selects and then using:



          (change)="updateGraph(city, scale.Name)"


          Option #2 on the .ts file you could just reference this.city and this.scale.Name in your updateGraph function.



          But make sure the (change) is on both of the select elements






          share|improve this answer





















          • 1





            I don't have nothing called 'state' actually on my code and well the scale I do have it but when I do a console log in the function it says that cannot read the property Name.

            – Christian Bawman
            Nov 13 '18 at 1:09











          • sorry i meant city!

            – JBoothUA
            Nov 13 '18 at 3:20











          • PS. unless you're planning on using $event you dont have to use it. but you can use it if you do not have access to the binded model. hope that makes sense

            – JBoothUA
            Nov 13 '18 at 3:22
















          0














          I think the issue is with



          (change)="updateGraph($event)"


          being placed on a div element, as divs do not have onChange events, unless the card-body is somehow providing that.



          I would move that call to both of the select elements, so it will be triggered on either of the select's onChange events.



          You can also pass in whatever variable you need to, or just reference them from the .ts file.



          Option #1 looks like it would be, moving it to the selects and then using:



          (change)="updateGraph(city, scale.Name)"


          Option #2 on the .ts file you could just reference this.city and this.scale.Name in your updateGraph function.



          But make sure the (change) is on both of the select elements






          share|improve this answer





















          • 1





            I don't have nothing called 'state' actually on my code and well the scale I do have it but when I do a console log in the function it says that cannot read the property Name.

            – Christian Bawman
            Nov 13 '18 at 1:09











          • sorry i meant city!

            – JBoothUA
            Nov 13 '18 at 3:20











          • PS. unless you're planning on using $event you dont have to use it. but you can use it if you do not have access to the binded model. hope that makes sense

            – JBoothUA
            Nov 13 '18 at 3:22














          0












          0








          0







          I think the issue is with



          (change)="updateGraph($event)"


          being placed on a div element, as divs do not have onChange events, unless the card-body is somehow providing that.



          I would move that call to both of the select elements, so it will be triggered on either of the select's onChange events.



          You can also pass in whatever variable you need to, or just reference them from the .ts file.



          Option #1 looks like it would be, moving it to the selects and then using:



          (change)="updateGraph(city, scale.Name)"


          Option #2 on the .ts file you could just reference this.city and this.scale.Name in your updateGraph function.



          But make sure the (change) is on both of the select elements






          share|improve this answer















          I think the issue is with



          (change)="updateGraph($event)"


          being placed on a div element, as divs do not have onChange events, unless the card-body is somehow providing that.



          I would move that call to both of the select elements, so it will be triggered on either of the select's onChange events.



          You can also pass in whatever variable you need to, or just reference them from the .ts file.



          Option #1 looks like it would be, moving it to the selects and then using:



          (change)="updateGraph(city, scale.Name)"


          Option #2 on the .ts file you could just reference this.city and this.scale.Name in your updateGraph function.



          But make sure the (change) is on both of the select elements







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 3:21

























          answered Nov 13 '18 at 0:59









          JBoothUAJBoothUA

          845425




          845425








          • 1





            I don't have nothing called 'state' actually on my code and well the scale I do have it but when I do a console log in the function it says that cannot read the property Name.

            – Christian Bawman
            Nov 13 '18 at 1:09











          • sorry i meant city!

            – JBoothUA
            Nov 13 '18 at 3:20











          • PS. unless you're planning on using $event you dont have to use it. but you can use it if you do not have access to the binded model. hope that makes sense

            – JBoothUA
            Nov 13 '18 at 3:22














          • 1





            I don't have nothing called 'state' actually on my code and well the scale I do have it but when I do a console log in the function it says that cannot read the property Name.

            – Christian Bawman
            Nov 13 '18 at 1:09











          • sorry i meant city!

            – JBoothUA
            Nov 13 '18 at 3:20











          • PS. unless you're planning on using $event you dont have to use it. but you can use it if you do not have access to the binded model. hope that makes sense

            – JBoothUA
            Nov 13 '18 at 3:22








          1




          1





          I don't have nothing called 'state' actually on my code and well the scale I do have it but when I do a console log in the function it says that cannot read the property Name.

          – Christian Bawman
          Nov 13 '18 at 1:09





          I don't have nothing called 'state' actually on my code and well the scale I do have it but when I do a console log in the function it says that cannot read the property Name.

          – Christian Bawman
          Nov 13 '18 at 1:09













          sorry i meant city!

          – JBoothUA
          Nov 13 '18 at 3:20





          sorry i meant city!

          – JBoothUA
          Nov 13 '18 at 3:20













          PS. unless you're planning on using $event you dont have to use it. but you can use it if you do not have access to the binded model. hope that makes sense

          – JBoothUA
          Nov 13 '18 at 3:22





          PS. unless you're planning on using $event you dont have to use it. but you can use it if you do not have access to the binded model. hope that makes sense

          – JBoothUA
          Nov 13 '18 at 3:22


















          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%2f53272223%2fpass-data-on-parent-change-angular-7%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

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation