我在同一TypeScript(.ts)文件中使用两个类。
有两个类 Class1 和 Class2 。我在 Class2 中调用 Class1 函数以根据 Class2 的响应设置值。我可以在同一功能中(通过控制台)设置值,但无法在 Class1 函数中获取值。
下面是我的 Class1 :
@Component({
selector: 'app-new-mission',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './new-mission.component.html',
styleUrls: ['./new-mission.component.css']
})
export class NewMissionComponent implements OnInit {
public data_option_id:any ;
public data_option_label:any;
dataOptionLabelAdd(dataoptionid, label) {
this.data_option_id = dataoptionid;
this.data_option_label = label;
console.log(this.data_option_id) // I can see values setted here
console.log(this.data_option_label) // I can see values setted here
addDataField() {
console.log(this.data_option_id) // I want to get values here
console.log(this.data_option_label)
}
} }
下面是我的 Class2 :
@Component({
selector: 'modal-list.component',
templateUrl: 'modal-list.component.html',
})
export class FormDialogComponent {
// This is to get object from **Class1**
private missionObj: any;
constructor(
private formb: FormBuilder,private
newmissionServiceM:NewMissionService,
public dialogRef: MatDialogRef<FormDialogComponent>,
//adding parameters for new mission component.
private http: HttpClient,private route: ActivatedRoute,private
router: Router,private fb: FormBuilder,
private commonService:CommonService,
private newmissionService:NewMissionService,
private localeService: BsLocaleService,
private viewContainerRef: ViewContainerRef,
private ref: ChangeDetectorRef,
private atp: AmazingTimePickerService,
public datepipe: DatePipe,public dialog: MatDialog
) { }
// In this function im calling **Class1** function
addDataFieldOptions() {
let dropdownId = 1;
let data_type_label = "label";
//creating object for NewMissionComponent
this.missionObj = new NewMissionComponent(this.http, this.route, this.router, this.fb, this.commonService, this.newmissionService, this.localeService, this.viewContainerRef, this.ref, this.atp, this.datepipe, this.dialog);
// Calling function
this.missionObj.dataOptionLabelAdd(dropdownId, data_type_label);
我可以调用 Class2 中的 Class1 的dataOptionLabelAdd
函数。在调用函数(dataOptionLabelAdd)时,我可以看到分配的值(在控制台中)。但是我无法在addDataField
函数中获取值。我不知道是什么问题。或者,请向我建议其他方法来实现这一目标。预先感谢。