Pass data on parent change angular 7
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
add a comment |
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
add a comment |
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
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
angular angular7
edited Nov 28 '18 at 18:49
Goncalo Peres
1,4541519
1,4541519
asked Nov 13 '18 at 0:52
Christian BawmanChristian Bawman
235
235
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
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
add a comment |
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
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
edited Nov 13 '18 at 14:55
answered Nov 13 '18 at 2:17
Code-EZCode-EZ
3,38593052
3,38593052
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
edited Nov 13 '18 at 3:21
answered Nov 13 '18 at 0:59
JBoothUAJBoothUA
875425
875425
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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