微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将下拉 angular 的选定值从一个组件保留到另一个组件 Angular

如何解决将下拉 angular 的选定值从一个组件保留到另一个组件 Angular

嗨,我有一个名为 switchercomponent 的常见下拉列表,它用于两个组件中,一个名为 detail,另一个名为 edit 所以 detailComponent 和 editComponent 有相同的下拉组件,名为 switcherComponent 。 detailComponent 和 editComponent 是 masterComponent 的子级

在 detailComponent 和 editComponent 中,我在各自的模板中调用了如下所示的开关组件

 <switcher (onChange)="studentFileChanged($event)" ></switcher>

在detailComponent 中,当用户从switchcomponent 中进行选择然后单击edit button 时,editcomponent 被调用,我们有相同的switchercomponent。我的问题是,当用户从detailcomponent 中的下拉切换器中选择一个值,然后单击edit 按钮时,从detailcomponent 中的switcher 组件中select drop 的值不会保留在editcomponent 中。我已经使用 this.onChange.emit() 来更新切换器组件,但它似乎不起作用。我该如何解决这个问题。

在切换器组件中,我有以下代码

@Output() public onChange = new EventEmitter<StudentFileviewmodel>();
  public studentFilesOptions: SelectOption[] = [];
  public form: FormGroup;
  public selectedStudentFileId: number;
  public StudentFiles: StudentFileviewmodel[];
  public newStudentFiles: StudentFileviewmodel[];
  public selectedStudentFile: StudentFileviewmodel;


  private readonly predicates: (keyof StudentFileviewmodel)[] = ['name'];
  private destroy$ = new Subject<boolean>();

  constructor(private store: Store,private managementService: ManagementService,public changeDetectorReference: ChangeDetectorRef,) {}

  public ngOnDestroy(): void {
    this.destroy$.next(true);
    this.destroy$.complete();
  }

  public ngOnInit(): void {
     this.form = new FormGroup({
      studentFileCtlr: new FormControl(),});
    this.store
      .select(filesGetData)
      .pipe(takeuntil(this.destroy$))
      .subscribe(studentFiles => {
       this.studentFiles = this.getSortedStudentFiles(studenFiles);
       this.setStudentFilesSelectOptions();
       this.selectedStudentFile = 
       this.managementService.getIntialStudentFile(this.studentFiles);
       this.selectedStudentFileId = this.selectedStudentFile.id;
      this.updateSudentFile();
    })
 
      
}

  private setStudentFilesSelectOptions= (): void =>{
    this.studentFilesOptions = [];
    this.studentFiles.forEach(studentFile => {
      this.studentFilesOptions.push({ label: `${studentFile.name}`,value: studentFile.id });
    });
 }

  private getSortedStudentFiles= (studentFiles: StudentFileviewmodel[]): StudentFileviewmodel[] =>
  ArrayUtil.sortBy(studentFiles,this.predicates,[Order.Ascending]);


 public updateStudentFile(): void {
   
    this.form.get('studentFileCtlr').valueChanges.subscribe( value => {
      this.selectedStudentFile = value;
      this.selectedStudentFile = this.managementService.getSelectedStudentFile(value,this.studentFiles);
      this.selectedStudentFileId = this.selectedStudentFile.id;

      this.form.get("studentFileCtlr").patchValue(this.selectedStudentFile);
    
    });
     this.onChange.emit(this.selectedStudentFile);
  }


在服务管理Service.ts

   public getIntialStudentFile(studentFiles: StudentFileviewmodel[]): StudentFileviewmodel {
    return studentFileId ? studentFiles.find(studentFile => studentFile.id === studentFileId) : studentFiles[0];
  }

  public getSelectedStudentFile(studentFileId:number,studentFiles: StudentFileviewmodel[]): StudentFileviewmodel {
    return studentFileId ? studentFiles.find(studentFile => studentFile.id === studentFileId) : studentFiles[0];
  }

在切换器组件模板中

<form [formGroup]="form">
<div class="d-flex align-items-center">
 
  <select
    class="form-control studentfile-dropdown"
    (change)="updateStudentFile()"
    [items]="studentFilesOptions"
    [(ngModel)]="selectedStudentFileId"
    [searchable]="true"
    [clearable]="false"
    bindLabel="label"
    bindValue="value"
    formControlName="studentFileCtlr"
  ></select>
</div>
</form>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?