Angular 将数据传递到父更改7

Angular 将数据传递到父更改7,angular,angular7,Angular,Angular7,我有两个下拉列表,当其中一个发生更改时,我需要将它们的值传递给组件中的updateGraph函数 <div class="card-body" (change)="updateGraph($event)"> <div class="row"> <label>City</label> &

我有两个下拉列表,当其中一个发生更改时,我需要将它们的值传递给组件中的updateGraph函数

   <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>

我认为问题出在你身上

(change)="updateGraph($event)"
放置在div元素上,因为div没有onChange事件,除非卡体以某种方式提供了onChange事件

我会将该调用移动到两个select元素,这样它就会在select的onChange事件中被触发

您还可以传入任何需要的变量,或者只是从.ts文件中引用它们

选项1看起来是这样的,将其移动到“选择”,然后使用:

(change)="updateGraph(city, scale.Name)"
选项2在.ts文件中,您可以在updateGraph函数中引用this.city和this.scale.Name


但要确保更改同时出现在两个选择元素上,您必须在两个选择元素上应用updateGraph,如下所示

<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>
您可以从组件中检查条件

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

实际上,我的代码中没有所谓的“state”,我也有,但当我在函数中做控制台日志时,它会说无法读取属性名称。对不起,我是说city!另外,除非你打算使用$event,否则你不必使用它。但是,如果您没有访问绑定模型的权限,则可以使用它。希望能让senseI理解这个概念,它是这样工作的,但是updateGraph函数调用服务,服务调用API,但是这个API需要接收两个参数。好吧,假设我已经选择了一个城市,但现在我选择了一个比例,我可以从这里获得比例值,但如何获得我之前在该活动中选择的城市?@ChristianBawman您可以创建一个单独的属性,用于在组件中保存城市和比例的所选值,如stackblitz演示中所示。因此,财产的价值将始终存在。如果你不清楚的想法,我可以更新stackblitz,增加一个dropdown@ChristianBawman更新了演示应用程序。请让我知道如果你有任何问题只是令人惊讶,它的工作。我没有使用按钮,而是直接调用函数updateGraph,经过几次验证,我能够得到该函数中的值,非常感谢,先生!
 updateGraph('scale',$event.target.value)  //for scale 
 updateGraph('city',$event.target.value) //city
updateGraph(type,value){
if(type==='scale'){
  //value for scale 
}
else if(type==='city'){
//value for city
}
}